Search in sources :

Example 1 with ValueWrapper

use of org.springframework.cache.Cache.ValueWrapper in project cu-kfs by CU-CommunityApps.

the class AttributeValidationHelper method getAttributeDefinitionByName.

protected KimAttribute getAttributeDefinitionByName(String attributeName) {
    CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.CACHE_NAME);
    Cache cache = cm.getCache(KimAttribute.CACHE_NAME);
    // CU Customization (KFSPTS-23531): Build a more specific cache key
    String cacheKey = MessageFormat.format(ATTRIBUTE_BY_NAME_CACHE_KEY_PATTERN, attributeName);
    ValueWrapper valueWrapper = cache.get(cacheKey);
    if (valueWrapper != null) {
        return (KimAttribute) valueWrapper.get();
    }
    Map<String, String> criteria = new HashMap<>();
    criteria.put(KRADPropertyConstants.ATTRIBUTE_NAME, attributeName);
    List<KimAttribute> attributeImpls = (List<KimAttribute>) getBusinessObjectService().findMatching(KimAttribute.class, criteria);
    KimAttribute attribute = null;
    if (!attributeImpls.isEmpty()) {
        attribute = attributeImpls.get(0);
    }
    cache.put(cacheKey, attribute);
    return attribute;
}
Also used : ValueWrapper(org.springframework.cache.Cache.ValueWrapper) HashMap(java.util.HashMap) CacheManager(org.springframework.cache.CacheManager) KimAttribute(org.kuali.kfs.kim.impl.common.attribute.KimAttribute) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.springframework.cache.Cache)

Example 2 with ValueWrapper

use of org.springframework.cache.Cache.ValueWrapper in project cu-kfs by CU-CommunityApps.

the class AttributeValidationHelper method getAttributeDefinitionById.

protected KimAttribute getAttributeDefinitionById(String id) {
    CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.CACHE_NAME);
    Cache cache = cm.getCache(KimAttribute.CACHE_NAME);
    String cacheKey = "{" + KimAttribute.CACHE_NAME + "}id=" + id;
    ValueWrapper valueWrapper = cache.get(cacheKey);
    if (valueWrapper != null) {
        return (KimAttribute) valueWrapper.get();
    }
    KimAttribute attribute = getBusinessObjectService().findBySinglePrimaryKey(KimAttribute.class, id);
    cache.put(cacheKey, attribute);
    return attribute;
}
Also used : ValueWrapper(org.springframework.cache.Cache.ValueWrapper) CacheManager(org.springframework.cache.CacheManager) KimAttribute(org.kuali.kfs.kim.impl.common.attribute.KimAttribute) Cache(org.springframework.cache.Cache)

Example 3 with ValueWrapper

use of org.springframework.cache.Cache.ValueWrapper in project spring-framework by spring-projects.

the class CaffeineCacheTests method testLoadingCacheGet.

@Test
void testLoadingCacheGet() {
    Object value = new Object();
    CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder().build(key -> value));
    ValueWrapper valueWrapper = loadingCache.get(new Object());
    assertThat(valueWrapper).isNotNull();
    assertThat(valueWrapper.get()).isEqualTo(value);
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Cache(org.springframework.cache.Cache) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ValueWrapper(org.springframework.cache.Cache.ValueWrapper) AbstractValueAdaptingCacheTests(org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests) ValueWrapper(org.springframework.cache.Cache.ValueWrapper) Test(org.junit.jupiter.api.Test)

Aggregations

Cache (org.springframework.cache.Cache)3 ValueWrapper (org.springframework.cache.Cache.ValueWrapper)3 KimAttribute (org.kuali.kfs.kim.impl.common.attribute.KimAttribute)2 CacheManager (org.springframework.cache.CacheManager)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 AbstractValueAdaptingCacheTests (org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests)1