From 2667082ae0c817df7a9a7c29efbeace909702c88 Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Tue, 7 May 2024 22:56:41 +0200 Subject: [PATCH] CacheRemoveAll Done --- ...CacheClearAll.java => CacheRemoveAll.java} | 34 +++--------- .../compat/bifs/cache/CacheRemoveAllTest.java | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+), 26 deletions(-) rename src/main/java/ortus/boxlang/modules/compat/bifs/cache/{CacheClearAll.java => CacheRemoveAll.java} (54%) create mode 100644 src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAllTest.java diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearAll.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAll.java similarity index 54% rename from src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearAll.java rename to src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAll.java index 3c85efb..cd581e0 100644 --- a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheClearAll.java +++ b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAll.java @@ -18,9 +18,6 @@ import ortus.boxlang.runtime.bifs.BIF; import ortus.boxlang.runtime.bifs.BoxBIF; -import ortus.boxlang.runtime.cache.filters.ICacheKeyFilter; -import ortus.boxlang.runtime.cache.filters.RegexFilter; -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; @@ -30,48 +27,33 @@ import ortus.boxlang.runtime.validation.Validator; @BoxBIF -public class CacheClearAll extends BIF { +public class CacheRemoveAll extends BIF { private static final Validator cacheExistsValidator = new CacheExistsValidator(); /** * Constructor */ - public CacheClearAll() { + public CacheRemoveAll() { super(); declaredArguments = new Argument[] { - new Argument( true, Argument.STRING, Key.filter ), - new Argument( false, Argument.STRING, Key.cacheName, Key._DEFAULT, Set.of( cacheExistsValidator ) ), - new Argument( false, Argument.BOOLEAN, Key.useRegex, false ), + new Argument( false, Argument.STRING, Key.cacheName, Key._DEFAULT, Set.of( cacheExistsValidator ) ) }; } /** - * Clear multiples keys in the cache based on a filter. - * If no cache name is provided, the default cache is used. - * A filter is a simple string that can contain wildcards and will leverage the {@link WildcardFilter} to match keys. - * Or you can use a regex filter by setting the {@code useRegex} argument to true. + * Removes all stored objects in a cache region. If no cache region is specified, objects in the default region are removed. * * @param context The context in which the BIF is being invoked. * @param arguments Argument scope for the BIF. * - * @argument.filter The filter to apply to the keys, this can be a simple Wildcard filter or a regex filter. - * * @argument.cacheName The name of the cache to get the keys from. Default is the default cache. * - * @argument.useRegex If true, the filter will be treated as a full regular expression filter. Default is false. - * - * @return True if the keys were cleared, false otherwise + * @return nothing */ public Boolean _invoke( IBoxContext context, ArgumentsScope arguments ) { - ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) ); - String filter = arguments.getAsString( Key.filter ); - Boolean useRegex = arguments.getAsBoolean( Key.useRegex ); - - // Build the right filter - ICacheKeyFilter keyFilter = useRegex ? new RegexFilter( filter ) : new WildcardFilter( filter ); - - // Filter the keys - return cache.clearAll( keyFilter ); + ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) ); + cache.clearAll(); + return true; } } diff --git a/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAllTest.java b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAllTest.java new file mode 100644 index 0000000..61acee1 --- /dev/null +++ b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheRemoveAllTest.java @@ -0,0 +1,53 @@ +/** + * [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 CacheRemoveAllTest extends BaseCacheTest { + + @Test + @DisplayName( "Can remove all from the default cache" ) + public void canRemoveDefaultCache() { + runtime.executeSource( + """ + cacheRemoveAll(); + """, + context ); + + assertThat( boxCache.lookup( "tdd" ) ).isFalse(); + assertThat( boxCache.lookup( "bdd" ) ).isFalse(); + } + + @Test + @DisplayName( "Can remove all from a specific cache" ) + public void canRemoveSpecificCache() { + runtime.executeSource( + """ + cacheRemoveAll( "default" ); + """, + context ); + + assertThat( boxCache.lookup( "tdd" ) ).isFalse(); + assertThat( boxCache.lookup( "bdd" ) ).isFalse(); + } + +}