diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java index 072daf2..08740ea 100644 --- a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java +++ b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java @@ -28,6 +28,7 @@ import ortus.boxlang.runtime.validation.Validator; @BoxBIF +@BoxBIF( alias = "cacheIdExists" ) public class CacheIdExists extends BIF { private static final Validator cacheExistsValidator = new CacheExistsValidator(); diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExists.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExists.java new file mode 100644 index 0000000..878426b --- /dev/null +++ b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExists.java @@ -0,0 +1,51 @@ +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" + * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package ortus.boxlang.modules.compat.bifs.cache; + +import ortus.boxlang.runtime.bifs.BIF; +import ortus.boxlang.runtime.bifs.BoxBIF; +import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.scopes.ArgumentsScope; +import ortus.boxlang.runtime.scopes.Key; +import ortus.boxlang.runtime.types.Argument; + +@BoxBIF +public class CacheRegionExists extends BIF { + + /** + * Constructor + */ + public CacheRegionExists() { + super(); + declaredArguments = new Argument[] { + new Argument( true, Argument.STRING, Key.region ) + }; + } + + /** + * Checks if the cache region exists. + * + * @param context The context in which the BIF is being invoked. + * @param arguments Argument scope for the BIF. + * + * @argument.region The cache region to check for existence. + * + * @return True if the cache region exists, false otherwise. + */ + public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { + // Get the requested cache + return cacheService.hasCache( Key.of( arguments.getAsString( Key.region ) ) ); + } +} diff --git a/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExistsTest.java b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExistsTest.java new file mode 100644 index 0000000..b028a50 --- /dev/null +++ b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRegionExistsTest.java @@ -0,0 +1,49 @@ +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ortus.boxlang.modules.compat.bifs.cache; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CacheRegionExistsTest extends BaseCacheTest { + + @Test + @DisplayName( "Can get the default cache session" ) + public void canGetCacheSession() { + runtime.executeSource( + """ + result = cacheRegionExists( "default" ); + """, + context ); + assertThat( variables.get( result ) ).isEqualTo( true ); + } + + @Test + @DisplayName( "Can get the specific cache session" ) + public void canGetSpecificCacheSession() { + runtime.executeSource( + """ + result = cacheRegionExists( "tessdafdsa" ); + """, + context ); + assertThat( variables.get( result ) ).isEqualTo( false ); + } + +}