Skip to content

Commit

Permalink
CacheRemoveAll Done
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed May 7, 2024
1 parent 207ea49 commit 2667082
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 2667082

Please sign in to comment.