Java Code Examples for org.springframework.cache.Cache.ValueWrapper

The following examples show how to use org.springframework.cache.Cache.ValueWrapper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: oauth2-server   Source File: CaptchaServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public boolean checkCaptchaTimes(CachesEnum cachesEnum, String key) {
    ValueWrapper valueWrapper = cacheManager.getCache(CachesEnum.CaptchaTimesCache.name()).get(key);
    if (valueWrapper != null) {
        int times = Integer.valueOf(String.valueOf(valueWrapper.get()));
        if (times < captchaMaxTimes) {
            cacheManager.getCache(CachesEnum.CaptchaTimesCache.name()).put(key, times + 1);
            return true;
        } else {
            log.debug("验证码达到最大尝试次数:" + cachesEnum + "_" + key);
            removeCaptcha(cachesEnum, key);
            removeCaptcha(CachesEnum.CaptchaTimesCache, key);
            return false;
        }
    } else {
        cacheManager.getCache(CachesEnum.CaptchaTimesCache.name()).put(key, 1);
        return true;
    }
}
 
Example 2
Source Project: conf4j   Source File: CachingTypeConverter.java    License: MIT License 6 votes vote down vote up
/**
 * Converts String to the target type.
 * This method checks whether the cache contains the converted value, and if not, obtains it from {@link #typeConverter},
 * stores conversion result in the cache and provides the result.
 *
 * @param type  actual type definition.
 * @param value string representation of the value which is converted to {@code T}.
 *              In case it is {@code null}, converter should return either {@code null} or a value
 *              that is equivalent e.g. an empty list.
 * @return value converted to type {@code T}.
 * @param attributes additional meta-data attributes which may be used by converter. It can be {@code null}.
 * @throws IllegalArgumentException when {@code value} cannot be converted to {@code T}.
 * @throws NullPointerException     when {@code type} is {@code null}.
 */
@SuppressWarnings("unchecked")
@Override
public T fromString(Type type, String value, Map<String, String> attributes) {
    requireNonNull(type, "type cannot be null");

    SimpleKey key = new SimpleKey(type, attributes, value);
    ValueWrapper valueWrapper = cache.get(key);

    if (valueWrapper != null) {
        return (T) valueWrapper.get();
    } else {
        T val = typeConverter.fromString(type, value, attributes);
        valueWrapper = cache.putIfAbsent(key, val);
        if (valueWrapper == null) {
            return val;
        } else {
            return (T) valueWrapper.get();
        }
    }
}
 
Example 3
Source Project: onetwo   Source File: SimpleCacheWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T get(String key){
	if(!canCache())
		return null;
	ValueWrapper value = cache.get(key);
	if(value==null)
		return null;
	CacheElement ce = (CacheElement)value.get();
	
	if(!ce.isIndate()){
		logger.info("clear cache by key: " + ce);
		return null;
	}
	
	if(logger.isInfoEnabled()){
		logger.info("get from cache by key: " + ce);
	}
	
	return (T)ce.getValue();
}
 
Example 4
protected KimAttribute getAttributeDefinitionById( String id ) {
      CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.Cache.NAME);
      Cache cache = cm.getCache(KimAttribute.Cache.NAME);
      String cacheKey = "id=" + id;
      ValueWrapper valueWrapper = cache.get( cacheKey );
      
      if ( valueWrapper != null ) {
          return (KimAttribute) valueWrapper.get();
      }

KimAttributeBo attributeImpl = KradDataServiceLocator.getDataObjectService().find( KimAttributeBo.class, id );
KimAttribute attribute = KimAttributeBo.to(attributeImpl);
cache.put( cacheKey, attribute );

  	return attribute;
  }
 
Example 5
protected KimAttribute getAttributeDefinitionByName( String attributeName ) {
    CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.Cache.NAME);
    Cache cache = cm.getCache(KimAttribute.Cache.NAME);
    String cacheKey = "name=" + attributeName;
    ValueWrapper valueWrapper = cache.get( cacheKey );
    
    if ( valueWrapper != null ) {
        return (KimAttribute) valueWrapper.get();
    }

    List<KimAttributeBo> attributeImpls = KradDataServiceLocator.getDataObjectService().findMatching( KimAttributeBo.class, QueryByCriteria.Builder.forAttribute(KRADPropertyConstants.ATTRIBUTE_NAME, attributeName).build()).getResults();
    KimAttribute attribute = null;
    if ( !attributeImpls.isEmpty() ) {
        attribute = KimAttributeBo.to(attributeImpls.get(0)); 
    }
    
    cache.put( cacheKey, attribute );

    return attribute;
}
 
Example 6
Source Project: oauth2-server   Source File: CaptchaServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public String getCaptcha(CachesEnum cachesEnum, String key) {
    if (!checkCaptchaTimes(cachesEnum, key)) {
        return null;
    }
    ValueWrapper valueWrapper = cacheManager.getCache(cachesEnum.name()).get(key);
    if (valueWrapper != null) {
        return String.valueOf(valueWrapper.get());
    } else {
        return null;
    }
}
 
Example 7
Source Project: xmanager   Source File: ShiroSpringCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public V get(K key) throws CacheException {
	if (logger.isTraceEnabled()) {
		logger.trace("Getting object from cache [" + this.cache.getName() + "] for key [" + key + "]key type:" + key.getClass());
	}
	ValueWrapper valueWrapper = cache.get(key);
	if (valueWrapper == null) {
		if (logger.isTraceEnabled()) {
			logger.trace("Element for [" + key + "] is null.");
		}
		return null;
	}
	return (V) valueWrapper.get();
}
 
Example 8
Source Project: Mastering-Spring-Cloud   Source File: CustomerService.java    License: MIT License 5 votes vote down vote up
public List<Account> findCustomerAccountsFallback(Long id) {
	ValueWrapper w = cacheManager.getCache("accounts").get(id);
	if (w != null) {
		return (List<Account>) w.get();
	} else {
		return new ArrayList<>();
	}
}
 
Example 9
Source Project: Mastering-Spring-Cloud   Source File: CustomerService.java    License: MIT License 5 votes vote down vote up
public Customer findCustomerWithAccountsFallback(Long customerId) {
	ValueWrapper w = cacheManager.getCache("customers").get(customerId);
	if (w != null) {
		return (Customer) w.get();
	} else {
		return new Customer();
	}
}
 
Example 10
Source Project: Mastering-Spring-Cloud   Source File: CustomerService.java    License: MIT License 5 votes vote down vote up
public List<Account> findCustomerAccountsFallback(Long id) {
	ValueWrapper w = cacheManager.getCache("accounts").get(id);
	if (w != null) {
		return (List<Account>) w.get();
	} else {
		return new ArrayList<>();
	}
}
 
Example 11
Source Project: Mastering-Spring-Cloud   Source File: CustomerService.java    License: MIT License 5 votes vote down vote up
public Customer findCustomerWithAccountsFallback(Long customerId) {
	ValueWrapper w = cacheManager.getCache("customers").get(customerId);
	if (w != null) {
		return (Customer) w.get();
	} else {
		return new Customer();
	}
}
 
Example 12
Source Project: super-cloudops   Source File: WebResourceCache.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public byte[] get(Object key) {
	ValueWrapper value = cache.get(key);
	return isNull(value) ? null : (byte[]) value.get();
}
 
Example 13
Source Project: rebuild   Source File: EhcacheDriver.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public String get(String key) {
	ValueWrapper w = cache().get(key);
	return w == null ? null : (String) w.get();
}
 
Example 14
Source Project: rebuild   Source File: EhcacheDriver.java    License: GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V getx(String key) {
	ValueWrapper w = cache().get(key);
	return w == null ? null : (V) w.get();
}
 
Example 15
@Override
public V get(K key) throws CacheException {
	ValueWrapper wrapper = this.delegator.get(key);
	return wrapper == null ? null : (V) wrapper.get();
}
 
Example 16
Source Project: hawkbit   Source File: DefaultDownloadIdCache.java    License: Eclipse Public License 1.0 4 votes vote down vote up
@Override
public DownloadArtifactCache get(final String downloadId) {
    final ValueWrapper valueWrapper = getCache().get(downloadId);
    return (valueWrapper == null) ? null : (DownloadArtifactCache) valueWrapper.get();
}