Skip to content

Commit

Permalink
finalized cacheclear
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed May 7, 2024
1 parent 9572d2d commit f3524aa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ) )
};
}
Expand All @@ -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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/ortus/boxlang/modules/compat/util/KeyDictionary.java
Original file line number Diff line number Diff line change
@@ -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" );

}
Original file line number Diff line number Diff line change
@@ -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 );
}

}

0 comments on commit f3524aa

Please sign in to comment.