org.apache.shiro.cache.CacheException Java Examples

The following examples show how to use org.apache.shiro.cache.CacheException. 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 File: SessionCacheManager.java    From easyweb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V get(K key) throws CacheException {
	if (key == null){
		return null;
	}
	
	V v = null;
	HttpServletRequest request = Servlets.getRequest();
	if (request != null){
		v = (V)request.getAttribute(cacheKeyName);
		if (v != null){
			return v;
		}
	}
	
	V value = null;
	value = (V)getSession().getAttribute(cacheKeyName);
	logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
	
	if (request != null && value != null){
		request.setAttribute(cacheKeyName, value);
	}
	return value;
}
 
Example #2
Source File: JedisIamCache.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Override
public Long timeToLive(CacheKey key, Object value) throws CacheException {
	notNull(value, "TTL key is null, please check configure");
	notNull(value, "TTL value is null, please check configure");

	byte[] realKey = key.getKey(name);
	// New create.
	if (!jedisCluster.exists(realKey)) {
		// key -> createTime
		jedisCluster.set(realKey, String.valueOf(value).getBytes(Charsets.UTF_8));
	}

	// Get last TTL expire
	Long lastTTL = jedisCluster.ttl(realKey);
	// Less than or equal to 0 means immediate expiration
	if (key.hasExpire()) {
		jedisCluster.expire(realKey, key.getExpire());
	}

	return lastTTL;
}
 
Example #3
Source File: JedisIamCache.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Override
public Object put(final CacheKey key, final Object value) throws CacheException {
	notNullOf(key, "key");
	notNullOf(value, "value");
	log.debug("Put key={}, value={}", key, value);

	byte[] data = null;
	if (key.getSerializer() != null) { // Using a custom serializer
		data = key.getSerializer().serialize(value);
	} else {
		data = serialize(value);
	}

	String ret = null;
	if (key.hasExpire()) {
		ret = jedisCluster.setex(key.getKey(name), key.getExpire(), data);
	} else {
		ret = jedisCluster.set(key.getKey(name), data);
	}
	return String.valueOf(ret).equalsIgnoreCase("nil") ? null : ret;
}
 
Example #4
Source File: RedisCache.java    From mumu with Apache License 2.0 6 votes vote down vote up
@Override
public V get(K key) throws CacheException {
    logger.debug("根据key从Redis中获取对象 key [" + key + "]");
    try {
        if (key == null) {
            return null;
        }else{
            byte[] rawValue = cache.get(getByteKey(key));
            V value = (V)JavaSerializeUtil.deserialize(rawValue);
            return value;
        }
    } catch (Throwable t) {
        throw new CacheException(t);
    }

}
 
Example #5
Source File: RedisCacheManager.java    From mumu with Apache License 2.0 6 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
    logger.debug("获取名称为: " + name + " 的RedisCache实例");

    Cache c = caches.get(name);

    if (c == null) {

        // create a new cache instance
        c = new RedisCache<K, V>(jedisClient, keyPrefix);

        // add it to the cache collection
        caches.put(name, c);
    }
    return c;
}
 
Example #6
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
	V value = null;
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key));
		logger.debug("remove {} {}", cacheKeyName, key);
	} catch (Exception e) {
		logger.warn("remove {} {}", cacheKeyName, key, e);
	} finally {
		Streams.safeClose(jedis);
	}
	return value;
}
 
Example #7
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
	if (key == null){
		return null;
	}
	
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		jedis.hset(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key), JedisUtils.toBytes(value));
		logger.debug("put {} {} = {}", cacheKeyName, key, value);
	} catch (Exception e) {
		logger.error("put {} {}", cacheKeyName, key, e);
	} finally {
		Streams.safeClose(jedis);
	}
	return value;
}
 
Example #8
Source File: JedisCacheManager.java    From easyweb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
	V value = null;
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key));
		logger.debug("remove {} {}", cacheKeyName, key);
	} catch (Exception e) {
		logger.warn("remove {} {}", cacheKeyName, key, e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
	return value;
}
 
Example #9
Source File: SessionCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V get(K key) throws CacheException {
    if (key == null){
        return null;
    }

    V v = null;
    HttpServletRequest request =  Mvcs.getReq();
    if (request != null){
        v = (V)request.getAttribute(cacheKeyName);
        if (v != null){
            return v;
        }
    }

    V value = null;
    value = (V)getSession().getAttribute(cacheKeyName);
    logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");

    if (request != null && value != null){
        request.setAttribute(cacheKeyName, value);
    }
    return value;
}
 
Example #10
Source File: ShiroRedisCache.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
    Set keys = this.redisTemplate.keys(this.cacheKeyPrefix + ":");
    if (null != keys && keys.size() > 0) {
        Iterator iterator = keys.iterator();
        this.redisTemplate.delete(iterator.next());
    }

}
 
Example #11
Source File: ShiroCacheManager.java    From jboot-admin with Apache License 2.0 5 votes vote down vote up
@Override
   public <K, V> Cache<K, V> getCache(String name) throws CacheException {
	Cache c = caches.get(name);
	if (c == null) {
		c = new ShiroCache<K, V>(name);
		caches.put(name, c);
	}
	return c;
}
 
Example #12
Source File: SessionCacheManager.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {

    V value = null;
    value = (V)getSession().removeAttribute(cacheKeyName);
    logger.debug("remove {} {}", cacheKeyName, key);

    return value;
}
 
Example #13
Source File: ShiroRedisCacheManager.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
    Cache cache=this.caches.get(name);
    if(null==cache){
        //TODO:自定义Shiro的Cache
      cache=new ShiroRedisCache<K,V>(cacheLive,cacheKeyPrefix,redisTemplate);
      this.caches.put(name,cache);
    }
    return cache;
}
 
Example #14
Source File: RedisCache.java    From mumu with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
    logger.debug("从redis中删除所有元素");
    try {
        cache.flushDB();
    } catch (Throwable t) {
        throw new CacheException(t);
    }
}
 
Example #15
Source File: CustomCache.java    From ShiroJwt with MIT License 5 votes vote down vote up
/**
 * 获取缓存
 */
@Override
public Object get(Object key) throws CacheException {
    if(Boolean.FALSE.equals(JedisUtil.exists(this.getKey(key)))){
        return null;
    }
    return JedisUtil.getObject(this.getKey(key));
}
 
Example #16
Source File: JedisIamCache.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(final CacheKey key) throws CacheException {
	notNullOf(key, "key");
	notNullOf(key.getValueClass(), "valueClass");
	log.debug("Get key={}", key);

	byte[] data = jedisCluster.get(key.getKey(name));
	if (key.getDeserializer() != null) { // Using a custom deserializer
		return key.getDeserializer().deserialize(data, key.getValueClass());
	}

	return deserialize(data, key.getValueClass());
}
 
Example #17
Source File: JedisCacheManager.java    From easyweb with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("clear {}", cacheKeyName);
	} catch (Exception e) {
		logger.error("clear {}", cacheKeyName, e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
}
 
Example #18
Source File: JedisIamCache.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	if (log.isDebugEnabled()) {
		log.debug("Clear name={}", name);
	}
	jedisCluster.hdel(name);
}
 
Example #19
Source File: JedisIamCache.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public Long incrementGet(CacheKey key, long incrBy) throws CacheException {
	byte[] realKey = key.getKey(name);
	// Increment
	Long res = jedisCluster.incrBy(key.getKey(name), incrBy);
	// Less than or equal to 0 means immediate expiration
	if (key.hasExpire()) {
		jedisCluster.expire(realKey, key.getExpire());
	}
	return res;
}
 
Example #20
Source File: RedisCache.java    From mumu with Apache License 2.0 5 votes vote down vote up
@Override
public int size() {
    try {
        Long longSize = new Long(cache.dbSize());
        return longSize.intValue();
    } catch (Throwable t) {
        throw new CacheException(t);
    }
}
 
Example #21
Source File: JbootShiroCacheManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
    try {
        return guavaCache.get(name, () -> new JbootShiroCache(name));
    } catch (ExecutionException e) {
        throw new CacheException(e);
    }
}
 
Example #22
Source File: JedisIamCacheManager.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Getting enhanced cache instance
 *
 * @param name
 * @return
 * @throws CacheException
 */
@Override
public IamCache getIamCache(String name) throws CacheException {
	String cacheName = getCacheName(name);
	IamCache cache = caching.get(cacheName);
	if (Objects.isNull(cache)) {
		caching.put(cacheName, (cache = new JedisIamCache(cacheName, jedisCluster)));
	}
	return cache;
}
 
Example #23
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 5 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	Object k=hashKey(key);
	hash.put((K)k, value);
	return value;
}
 
Example #24
Source File: SessionCacheManager.java    From easyweb with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
	
	V value = null;
	value = (V)getSession().removeAttribute(cacheKeyName);
	logger.debug("remove {} {}", cacheKeyName, key);
	
	return value;
}
 
Example #25
Source File: ShiroSpringCache.java    From xmanager with 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 #26
Source File: ShiroSpringCache.java    From xmanager with Apache License 2.0 5 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
	if (logger.isTraceEnabled()) {
		logger.trace("Putting object in cache [" + this.cache.getName() + "] for key [" + key + "]key type:" + key.getClass());
	}
	V previous = get(key);
	cache.put(key, value);
	return previous;
}
 
Example #27
Source File: ShiroSpringCache.java    From xmanager with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	if (logger.isTraceEnabled()) {
		logger.trace("Clearing all objects from cache [" + this.cache.getName() + "]");
	}
	cache.clear();
}
 
Example #28
Source File: ShiroSpringCacheManager.java    From xmanager with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
	if (logger.isTraceEnabled()) {
		logger.trace("Acquiring ShiroSpringCache instance named [" + name + "]");
	}
	org.springframework.cache.Cache cache = cacheManager.getCache(name);
	return new ShiroSpringCache<K, V>(cache);
}
 
Example #29
Source File: JedisCacheManager.java    From easyweb with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V get(K key) throws CacheException {
	if (key == null){
		return null;
	}
	
	V v = null;
	HttpServletRequest request = Servlets.getRequest();
	if (request != null){
		v = (V)request.getAttribute(cacheKeyName);
		if (v != null){
			return v;
		}
	}
	
	V value = null;
	Jedis jedis = null;
	try {
		jedis = JedisUtils.getResource();
		value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
		logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
	} catch (Exception e) {
		logger.error("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "", e);
	} finally {
		JedisUtils.returnResource(jedis);
	}
	
	if (request != null && value != null){
		request.setAttribute(cacheKeyName, value);
	}
	
	return value;
}
 
Example #30
Source File: MapCacheManager.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Cache<K, V> getCache(String cacheName) throws CacheException {
	Cache<K, V> cache = CACHES.get(cacheName);  
       if (null == cache) {  
       	cache = new MapCache<K, V>(cacheName);  
           CACHES.put(cacheName, cache);  
       }  
       return cache;  
}