@Nullable private Element lookup(Object key) { return this.cache.get(key); }
@Override @SuppressWarnings("unchecked") @Nullable public <T> T get(Object key, @Nullable Class<T> type) { Element element = this.cache.get(key); Object value = (element != null ? element.getObjectValue() : null); if (value != null && type != null && !type.isInstance(value)) { throw new IllegalStateException( "Cached value is not of required type [" + type.getName() + "]: " + value); } return (T) value; }
@SuppressWarnings("unchecked") @Override public <K, V> V get(K key) { final Element element = ehCache.get(key); return element == null ? null : (V) element.getObjectValue(); }
public <T> T get(String key, Supplier<T> compute) { Element element = ehcache.get(key); if (element != null) { return (T) element.getObjectValue(); } synchronized (key.intern()) { element = ehcache.get(key); if (element != null) { return (T) element.getObjectValue(); } T object = compute.get(); ehcache.put(new Element(key, object)); return object; } }
private Object getWithoutTransactionCheck(String key) { Element element = ehCache.get(key); if (element == null) { return null; } Object value = element.getObjectValue(); logUnsavedPersistentObjectInteraction(value, "PersistentObject {} without an id served out of cache."); return value; }
@Override @SuppressWarnings("unchecked") @Nullable public <T> T get(Object key, @Nullable Class<T> type) { Element element = this.cache.get(key); Object value = (element != null ? element.getObjectValue() : null); if (value != null && type != null && !type.isInstance(value)) { throw new IllegalStateException( "Cached value is not of required type [" + type.getName() + "]: " + value); } return (T) value; }
public UserDetails getUserFromCache(String username) { Element element = cache.get(username); if (logger.isDebugEnabled()) { logger.debug("Cache hit: " + (element != null) + "; username: " + username); } if (element == null) { return null; } else { return (UserDetails) element.getValue(); } }
public CasAuthenticationToken getByTicketId(final String serviceTicket) { final Element element = cache.get(serviceTicket); if (logger.isDebugEnabled()) { logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket); } return element == null ? null : (CasAuthenticationToken) element.getValue(); }
public ValueWrapper get(Object key) { Element element = this.cache.get(key); return (element != null ? new SimpleValueWrapper(element.getObjectValue()) : null); }
public MutableAcl getFromCache(ObjectIdentity objectIdentity) { Assert.notNull(objectIdentity, "ObjectIdentity required"); Element element = null; try { element = cache.get(objectIdentity); } catch (CacheException ignored) { } if (element == null) { return null; } return initializeTransientFields((MutableAcl) element.getValue()); }
public MutableAcl getFromCache(Serializable pk) { Assert.notNull(pk, "Primary key (identifier) required"); Element element = null; try { element = cache.get(pk); } catch (CacheException ignored) { } if (element == null) { return null; } return initializeTransientFields((MutableAcl) element.getValue()); }
@Override public Object extractPrincipal(X509Certificate cert) { try { Element element = cache.get(cert); if (element != null) { return element.getObjectValue(); } } catch (CacheException cacheException) { throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); } final Object principal = delegate.extractPrincipal(cert); cache.put(new Element(cert, principal)); return principal; } }
@Test public void getFromCacheObjectIdentity() throws Exception { when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl)); assertThat(myCache.getFromCache(acl.getId())).isEqualTo(acl); }
@Test public void getFromCacheSerializable() throws Exception { when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl)); assertThat(myCache.getFromCache(acl.getId())).isEqualTo(acl); }
@Test public void getFromCacheSerializablePopulatesTransient() throws Exception { when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl)); myCache.putInCache(acl); ReflectionTestUtils.setField(acl, "permissionGrantingStrategy", null); ReflectionTestUtils.setField(acl, "aclAuthorizationStrategy", null); MutableAcl fromCache = myCache.getFromCache(acl.getId()); assertThat(ReflectionTestUtils.getField(fromCache, "aclAuthorizationStrategy")) .isNotNull(); assertThat(ReflectionTestUtils.getField(fromCache, "permissionGrantingStrategy")) .isNotNull(); }
@Test public void getFromCacheObjectIdentityPopulatesTransient() throws Exception { when(cache.get(acl.getObjectIdentity())) .thenReturn(new Element(acl.getId(), acl)); myCache.putInCache(acl); ReflectionTestUtils.setField(acl, "permissionGrantingStrategy", null); ReflectionTestUtils.setField(acl, "aclAuthorizationStrategy", null); MutableAcl fromCache = myCache.getFromCache(acl.getObjectIdentity()); assertThat(ReflectionTestUtils.getField(fromCache, "aclAuthorizationStrategy")) .isNotNull(); assertThat(ReflectionTestUtils.getField(fromCache, "permissionGrantingStrategy")) .isNotNull(); }
@Test public void evictCacheSerializable() throws Exception { when(cache.get(acl.getObjectIdentity())) .thenReturn(new Element(acl.getId(), acl)); myCache.evictFromCache(acl.getObjectIdentity()); verify(cache).remove(acl.getId()); verify(cache).remove(acl.getObjectIdentity()); }
@Test public void evictCacheObjectIdentity() throws Exception { when(cache.get(acl.getId())).thenReturn(new Element(acl.getId(), acl)); myCache.evictFromCache(acl.getId()); verify(cache).remove(acl.getId()); verify(cache).remove(acl.getObjectIdentity()); } }
@Test public void testEhCacheFactoryBeanWithSelfPopulatingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(key -> key); assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof SelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); } finally { cacheManagerFb.destroy(); } }
@Test public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() { @Override public Object createEntry(Object key) { return key; } @Override public void updateEntryValue(Object key, Object value) { } }); assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); } finally { cacheManagerFb.destroy(); } }