Skip to content

Commit

Permalink
cacheGetSession compelted
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed May 7, 2024
1 parent cb6799f commit d8046e5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* [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 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.util.CacheExistsValidator;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.scopes.ArgumentsScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Argument;
import ortus.boxlang.runtime.validation.Validator;

@BoxBIF
public class CacheGetSession extends BIF {

private static final Validator cacheExistsValidator = new CacheExistsValidator();

/**
* Constructor
*/
public CacheGetSession() {
super();
declaredArguments = new Argument[] {
new Argument( false, Argument.STRING, KeyDictionary.objectType, Key._DEFAULT, Set.of( cacheExistsValidator ) ),
new Argument( false, Argument.BOOLEAN, KeyDictionary.isKey, false )
};
}

/**
* Lets you retrieve the underlying cache object to access additional cache functionality that is not implemented in the tag cfcache.
*
* @param context The context in which the BIF is being invoked.
* @param arguments Argument scope for the BIF.
*
*
*
*
* @return The cache instance
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
// Get the requested cache
return cacheService.getCache( arguments.getAsKey( KeyDictionary.objectType ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

public class KeyDictionary {

public static final Key filterOrTags = Key.of( "filterOrTags" );
public static final Key filterOrTags = Key.of( "filterOrTags" );
public static final Key objectType = Key.of( "objectType" );
public static final Key isKey = Key.of( "isKey" );

}
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;

import ortus.boxlang.runtime.cache.providers.ICacheProvider;

public class CacheGetSessionTest extends BaseCacheTest {

@Test
@DisplayName( "Can get the default cache session" )
public void canGetCacheSession() {
runtime.executeSource(
"""
result = cacheGetSession();
""",
context );
ICacheProvider cache = ( ICacheProvider ) variables.get( result );
assertThat( cache.getName().getName() ).isEqualTo( "default" );
}

@Test
@DisplayName( "Can get the specific cache session" )
public void canGetSpecificCacheSession() {
runtime.executeSource(
"""
result = cacheGetSession( "default" );
""",
context );
ICacheProvider cache = ( ICacheProvider ) variables.get( result );
assertThat( cache.getName().getName() ).isEqualTo( "default" );
}

}

0 comments on commit d8046e5

Please sign in to comment.