diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClear.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClear.java index bb1674f..b08fbb0 100644 --- a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClear.java +++ b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClear.java @@ -16,15 +16,19 @@ import java.util.Set; +import ortus.boxlang.modules.compat.util.KeyDictionary; import ortus.boxlang.runtime.bifs.BIF; import ortus.boxlang.runtime.bifs.BoxBIF; +import ortus.boxlang.runtime.cache.filters.WildcardFilter; import ortus.boxlang.runtime.cache.providers.ICacheProvider; import ortus.boxlang.runtime.cache.util.CacheExistsValidator; import ortus.boxlang.runtime.context.IBoxContext; +import ortus.boxlang.runtime.dynamic.casters.StringCaster; import ortus.boxlang.runtime.scopes.ArgumentsScope; import ortus.boxlang.runtime.scopes.Key; import ortus.boxlang.runtime.types.Argument; import ortus.boxlang.runtime.types.Array; +import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException; import ortus.boxlang.runtime.validation.Validator; @BoxBIF @@ -38,7 +42,7 @@ public class CacheClear extends BIF { public CacheClear() { super(); declaredArguments = new Argument[] { - new Argument( true, Argument.ANY, Key.id ), + new Argument( false, Argument.ANY, KeyDictionary.filterOrTags, "" ), new Argument( false, Argument.STRING, Key.cacheName, Key._DEFAULT, Set.of( cacheExistsValidator ) ) }; } @@ -57,16 +61,24 @@ public CacheClear() { * @return Boolean if a single id, or a Struct with the status of each key */ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { - ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) ); + ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) ); + Object filter = arguments.get( KeyDictionary.filterOrTags ); - // Build the right filter - // Single or multiple ids - if ( arguments.get( Key.id ) instanceof Array casteId ) { - // Convert the BoxLang array to an array of Strings - return cache.clear( ( String[] ) casteId.stream().map( Object::toString ).toArray() ); + // We only support strings not array + if ( filter instanceof Array ) { + throw new BoxRuntimeException( "We don't support an array of tags in our compat module" ); } + var filterOrTags = StringCaster.cast( filter ).trim(); + var size = cache.getSize(); - // Clear one - return cache.clear( arguments.getAsString( Key.id ) ); + // No Filter + if ( filterOrTags.isEmpty() ) { + cache.clearAll(); + } + + // Clear with filter + cache.clearAll( new WildcardFilter( filterOrTags ) ); + + return size; } } diff --git a/src/main/java/ortus/boxlang/modules/compat/util/KeyDictionary.java b/src/main/java/ortus/boxlang/modules/compat/util/KeyDictionary.java new file mode 100644 index 0000000..2029103 --- /dev/null +++ b/src/main/java/ortus/boxlang/modules/compat/util/KeyDictionary.java @@ -0,0 +1,23 @@ +/** + * [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.util; + +import ortus.boxlang.runtime.scopes.Key; + +public class KeyDictionary { + + public static final Key filterOrTags = Key.of( "filterOrTags" ); + +} diff --git a/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearTest.java b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearTest.java new file mode 100644 index 0000000..47635dc --- /dev/null +++ b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearTest.java @@ -0,0 +1,63 @@ +/** + * [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 CacheClearTest extends BaseCacheTest { + + @Test + @DisplayName( "Can clear the default cache" ) + public void canClearDefaultCache() { + runtime.executeSource( + """ + result = cacheClear(); + """, + context ); + + assertThat( boxCache.getSize() ).isEqualTo( 0 ); + } + + @Test + @DisplayName( "Can clear a specific cache" ) + public void canClearSpecificCache() { + runtime.executeSource( + """ + result = cacheClear( "", "default" ); + """, + context ); + + assertThat( boxCache.getSize() ).isEqualTo( 0 ); + } + + @Test + @DisplayName( "Can clear with a filter" ) + public void canClearWithFilter() { + runtime.executeSource( + """ + result = cacheClear( "bd*" ); + """, + context ); + + assertThat( boxCache.getSize() ).isEqualTo( 1 ); + } + +}