Java Code Examples for org.springframework.data.redis.core.BoundValueOperations#get()

The following examples show how to use org.springframework.data.redis.core.BoundValueOperations#get() . 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: SimpleRedisOperationService.java    From onetwo with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T getCache(String key, Supplier<CacheData<T>> cacheLoader) {
	String cacheKey = getCacheKey(key);
	BoundValueOperations<Object, Object> ops = boundValueOperations(cacheKey);
	Object value = ops.get();
	if (value==null && cacheLoader!=null) {
		CacheData<T> cacheData = getCacheData(ops, cacheKey, cacheLoader);
		if (cacheData==null) {
			throw new BaseException("can not load cache data.").put("key", key);
		}
		value = cacheData.getValue();
	} else {
		cacheStatis.addHit(1);
	}
	return (T)value;
}
 
Example 2
Source File: DistributedLockRedis.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
/**
     * 由于Redis是单线程模型,命令操作原子性,所以利用这个特性可以很容易的实现分布式锁。
     * 获得一个锁
     *
     * @param bizName     业务名
     * @param lockTimeout 线程占用锁的时间
     * @param unit        单位
     * @throws InterruptedException 锁可以被中断
     */
    public void lock(String bizName, int lockTimeout, TimeUnit unit) throws InterruptedException {
//        redisTemplate.getConnectionFactory().getConnection().setNX()
        String redisKey;
        if (StringUtils.isBlank(bizName)) {
            LOGGER.warn("has no bizName. is not recommended!");
            redisKey = DISTRIBUTED_LOCK_REDIS_KEY;
        } else {
            redisKey = DISTRIBUTED_LOCK_REDIS_KEY + bizName.trim();
        }
        BoundValueOperations<String, Long> valueOps = redisTemplate.boundValueOps(redisKey);
        while (true) {
            // https://redis.io/commands/setnx
            long currentTimeMillis = System.currentTimeMillis();
            long releaseLockTime = currentTimeMillis + unit.toMillis(lockTimeout) + 1;
            //这两个if else不能混写,因为多个相同类型的线程竞争锁时,在锁超时时,设置的超时时间是一样的
            if (valueOps.setIfAbsent(releaseLockTime)) {
                /**
                 * 第一次获取锁
                 */
                redisTemplate.expire(redisKey, lockTimeout, unit);
                return;
            } else if (currentTimeMillis > valueOps.get()) {
                /**
                 * 锁已经超时
                 */
                //如果其它线程占用锁,再重新设置的时间和原来时间的时间差,可以忽略
                Long lockCurrentValue = valueOps.getAndSet(releaseLockTime);
                //如果当前时间小于LockKey存放的时间,说明已经有其它线程加锁
                if (currentTimeMillis > lockCurrentValue) {
                    redisTemplate.expire(redisKey, lockTimeout, unit);
                    return;
                }
            } else {
                TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(100, 1000));
            }

        }

    }
 
Example 3
Source File: JwtTokenRedisStore.java    From onetwo with Apache License 2.0 5 votes vote down vote up
protected final JwtStoredTokenValue getJwtStoredTokenValue(String tokenId){
	String key = getStoreKey(tokenId);
	BoundValueOperations<String, JwtStoredTokenValue> ops = redisTemplate.boundValueOps(key);
	JwtStoredTokenValue storedTokenValue = ops.get();
	if(storedTokenValue==null){
		throw new ServiceException(JwtErrors.CM_ERROR_TOKEN);
	}
	return storedTokenValue;
}
 
Example 4
Source File: JsonRedisTemplateTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonRedisTemplate(){
	String key = "test:data";
	BoundValueOperations<Object, Object> ops = (BoundValueOperations<Object, Object>)jsonRedisTemplate.boundValueOps(key);
	JsonData data = new JsonData();
	data.setAge(111);
	data.setName("testName");
	ops.set(data);
	
	JsonData data2 = (JsonData)ops.get();
	assertThat(data2).isEqualTo(data);
	
	jsonRedisTemplate.delete(key);
}
 
Example 5
Source File: DefaultPersistenceService.java    From spring-boot-email-tools with Apache License 2.0 4 votes vote down vote up
protected EmailSchedulingData getOps(final String id) {
    //valueTemplate.
    BoundValueOperations<String, EmailSchedulingData> boundValueOps = valueTemplate.boundValueOps(id);
    EmailSchedulingData emailSchedulingData = boundValueOps.get();
    return emailSchedulingData;
}
 
Example 6
Source File: RedisCacheAspect.java    From jeecg with Apache License 2.0 4 votes vote down vote up
@Around("simplePointcut() && @annotation(ehcache)")
public Object aroundLogCalls(ProceedingJoinPoint joinPoint,Ehcache ehcache)throws Throwable {
	String targetName = joinPoint.getTarget().getClass().getName();
	String methodName = joinPoint.getSignature().getName();
	Object[] arguments = joinPoint.getArgs();

	String cacheKey ="";
	if(StringUtils.isNotBlank(ehcache.cacheName())){
		cacheKey=ehcache.cacheName();
	}else{
		cacheKey=getCacheKey(targetName, methodName, arguments);
	}
	
	Object result=null;
	BoundValueOperations<String,Object> valueOps = redisTemplate.boundValueOps(cacheKey);
	if(ehcache.eternal()){
		//永久缓存
		result = valueOps.get();
	}else{
		//临时缓存
		result = valueOps.get();
		valueOps.expire(20, TimeUnit.MINUTES);
	}

	if (result == null) {
		if ((arguments != null) && (arguments.length != 0)) {
			result = joinPoint.proceed(arguments);
		} else {
			result = joinPoint.proceed();
		}

		if(ehcache.eternal()){
			//永久缓存
			valueOps.set(result);
		}else{
			//临时缓存
			valueOps.set(result,20, TimeUnit.MINUTES);
		}
	}
	return result;
}