Java Code Examples for org.springframework.data.redis.connection.RedisConnection#set()
The following examples show how to use
org.springframework.data.redis.connection.RedisConnection#set() .
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: RedisUtils.java From rqueue with Apache License 2.0 | 6 votes |
public static int updateAndGetVersion( RedisConnectionFactory redisConnectionFactory, String versionDbKey, int defaultVersion) { RedisConnection connection = RedisConnectionUtils.getConnection(redisConnectionFactory); byte[] versionKey = versionDbKey.getBytes(); byte[] versionFromDb = connection.get(versionKey); if (SerializationUtils.isEmpty(versionFromDb)) { Long count = connection.eval( "return #redis.pcall('keys', 'rqueue-*')".getBytes(), ReturnType.INTEGER, 0); if (count != null && count > 0L) { int version = 1; connection.set(versionKey, String.valueOf(version).getBytes()); return version; } connection.set(versionKey, String.valueOf(defaultVersion).getBytes()); return defaultVersion; } return Integer.parseInt(new String(versionFromDb)); }
Example 2
Source File: CustomRedisTokenStore.java From microservices-platform with Apache License 2.0 | 6 votes |
@Override public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) { byte[] refreshKey = serializeKey(REFRESH + refreshToken.getValue()); byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + refreshToken.getValue()); byte[] serializedRefreshToken = serialize(refreshToken); RedisConnection conn = getConnection(); try { conn.openPipeline(); if (springDataRedis_2_0) { try { this.redisConnectionSet_2_0.invoke(conn, refreshKey, serializedRefreshToken); this.redisConnectionSet_2_0.invoke(conn, refreshAuthKey, serialize(authentication)); } catch (Exception ex) { throw new RuntimeException(ex); } } else { conn.set(refreshKey, serializedRefreshToken); conn.set(refreshAuthKey, serialize(authentication)); } expireRefreshToken(refreshToken, conn, refreshKey, refreshAuthKey); conn.closePipeline(); } finally { conn.close(); } }
Example 3
Source File: RedisAuthorizationCodeServices.java From springcloud-oauth2 with MIT License | 6 votes |
/** * 将随机生成的授权码存到redis中 * * @param code * @param authentication * @return void */ @Override protected void store(String code, OAuth2Authentication authentication) { byte[] serializedKey = serializeKey(AUTHORIZATION_CODE + code); byte[] serializedAuthentication = serialize(authentication); RedisConnection conn = getConnection(); try { conn.openPipeline(); conn.set(serializedKey, serializedAuthentication); conn.expire(serializedKey,expiration); conn.closePipeline(); } finally { conn.close(); } }
Example 4
Source File: RedisService.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * saveCache:(设置缓存-带有效期) * * @param key * @param value * @param seconds 有效时间 单位(秒) * @return * @author rufei.cn */ @RequestMapping("save") public long saveCache(String key, String value, long seconds) { logger.info("save:(设置缓存-带有效期) 开始 key={},seconds={}", key, seconds); long result = -1; if (StringUtil.isBlank(key)) { return result; } RedisConnection conn = getRedisConnection(); if (conn == null) { return result; } try { Boolean ret = conn.set(key.getBytes(), value.getBytes(), Expiration.seconds(seconds), RedisStringCommands.SetOption.UPSERT); if (ret) { result = 1L; } } catch (Exception e) { logger.error("saveCache:" + StringUtil.getExceptionMsg(e)); } logger.info("saveCache:(设置缓存-带有效期) 结束 key={},result={}", key, result); return result; }
Example 5
Source File: RedisService.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * getLock(分布式锁-正确方式) * * @param key 锁标识 * @param requestId 请求唯一标识,解锁需要带入 * @param expireTime 自动释放锁的时间 */ @RequestMapping("getLock") public Long getLock(String key, String requestId, long expireTime) { Long result = -1L; if (StringUtil.isBlank(key)) { return result; } if (StringUtil.isBlank(requestId)) { return result; } RedisConnection conn = getRedisConnection(); if (conn == null) { return result; } try { boolean ret = conn.set(key.getBytes(Charset.forName("UTF-8")), requestId.getBytes(Charset.forName("UTF-8")), Expiration.milliseconds(expireTime), RedisStringCommands.SetOption.SET_IF_ABSENT); if (ret) { result = 1L; } } catch (Exception e) { logger.error("getRedisLock(取分布式锁-正确方式)异常={}", StringUtil.getExceptionMsg(e)); } return result; }
Example 6
Source File: RedisSpringDataCache.java From jetcache with Apache License 2.0 | 6 votes |
@Override protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) { RedisConnection con = null; try { con = connectionFactory.getConnection(); CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite)); byte[] newKey = buildKey(key); Boolean result = con.set(newKey, valueEncoder.apply(holder), Expiration.from(expireAfterWrite, timeUnit), RedisStringCommands.SetOption.ifAbsent()); if (Boolean.TRUE.equals(result)) { return CacheResult.SUCCESS_WITHOUT_MSG; }/* else if (result == null) { return CacheResult.EXISTS_WITHOUT_MSG; } */ else { return CacheResult.EXISTS_WITHOUT_MSG; } } catch (Exception ex) { logError("PUT_IF_ABSENT", key, ex); return new CacheResult(ex); } finally { closeConnection(con); } }
Example 7
Source File: RedisClient.java From blog_demos with Apache License 2.0 | 6 votes |
/** * 更新缓存中的对象,也可以在redis缓存中存入新的对象 * * @param key * @param t * @param <T> */ public <T> void set(String key, T t) { byte[] keyBytes = getKey(key); RedisSerializer serializer = redisTemplate.getValueSerializer(); byte[] val = serializer.serialize(t); RedisConnection redisConnection = getConnection(); if(null!=redisConnection){ try { redisConnection.set(keyBytes, val); }finally { releaseConnection(redisConnection); } }else{ logger.error("1. can not get valid connection"); } }
Example 8
Source File: RedisClient.java From blog_demos with Apache License 2.0 | 6 votes |
/** * 更新缓存中的对象,也可以在redis缓存中存入新的对象 * * @param key * @param t * @param <T> */ public <T> void set(String key, T t) { byte[] keyBytes = getKey(key); RedisSerializer serializer = redisTemplate.getValueSerializer(); byte[] val = serializer.serialize(t); RedisConnection redisConnection = getConnection(); if(null!=redisConnection){ try { redisConnection.set(keyBytes, val); }finally { releaseConnection(redisConnection); } }else{ logger.error("1. can not get valid connection"); } }
Example 9
Source File: RootController.java From hcp-cloud-foundry-tutorials with Apache License 2.0 | 6 votes |
@RequestMapping(method = RequestMethod.GET) public @ResponseBody Result onRootAccess() { RedisConnection redisConnection = redisConnectionFactory.getConnection(); log.info("Setting key/value pair 'hello'/'world'"); redisConnection.set("hello".getBytes(), "world".getBytes()); log.info("Retrieving value for key 'hello': "); final String value = new String(redisConnection.get("hello".getBytes())); Result result = new Result(); result.setStatus("Succesfully connected to Redis and retrieved the key/value pair that was inserted"); RedisObject redisObject = new RedisObject(); redisObject.setKey("hello"); redisObject.setValue(value); final ArrayList<RedisObject> redisObjects = new ArrayList<RedisObject>(); redisObjects.add(redisObject); result.setRedisObjects(redisObjects); return result; }
Example 10
Source File: RedisLockProvider.java From ShedLock with Apache License 2.0 | 6 votes |
@Override public void unlock() { Expiration keepLockFor = getExpiration(lockConfiguration.getLockAtLeastUntil()); RedisConnection redisConnection = null; // lock at least until is in the past if (keepLockFor.getExpirationTimeInMilliseconds() <= 0) { try { redisConnection = redisConnectionFactory.getConnection(); redisConnection.del(key.getBytes()); } catch (Exception e) { throw new LockException("Can not remove node", e); } finally { close(redisConnection); } } else { try { redisConnection = redisConnectionFactory.getConnection(); redisConnection.set(key.getBytes(), buildValue(lockConfiguration.getLockAtMostUntil()), keepLockFor, SetOption.SET_IF_PRESENT); } finally { close(redisConnection); } } }
Example 11
Source File: RedisUtils.java From rqueue with Apache License 2.0 | 4 votes |
public static void setVersion( RedisConnectionFactory connectionFactory, String versionKey, int version) { RedisConnection connection = RedisConnectionUtils.getConnection(connectionFactory); byte[] versionKeyBytes = versionKey.getBytes(); connection.set(versionKeyBytes, String.valueOf(version).getBytes()); }
Example 12
Source File: StringDataRedisTest.java From tac with MIT License | 3 votes |
@Test public void testIncre() { RedisConnection connection = template.getConnectionFactory().getConnection(); byte[] key = Bytes.toBytes("ljinshuan123"); Long incr = connection.incr(key); byte[] bytes = connection.get(key); connection.set(key, bytes); connection.incr(key); bytes = connection.get(key); }
Example 13
Source File: TestRedis.java From BigDataArchitect with Apache License 2.0 | 2 votes |
public void testRedis(){ // stringRedisTemplate.opsForValue().set("hello01","china"); // // System.out.println(stringRedisTemplate.opsForValue().get("hello01")); RedisConnection conn = redisTemplate.getConnectionFactory().getConnection(); conn.set("hello02".getBytes(),"mashibing".getBytes()); System.out.println(new String(conn.get("hello02".getBytes()))); // HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash(); // hash.put("sean","name","zhouzhilei"); // hash.put("sean","age","22"); // // System.out.println(hash.entries("sean")); Person p = new Person(); p.setName("zhangsan"); p.setAge(16); // stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)); Jackson2HashMapper jm = new Jackson2HashMapper(objectMapper, false); stringRedisTemplate.opsForHash().putAll("sean01",jm.toHash(p)); Map map = stringRedisTemplate.opsForHash().entries("sean01"); Person per = objectMapper.convertValue(map, Person.class); System.out.println(per.getName()); stringRedisTemplate.convertAndSend("ooxx","hello"); RedisConnection cc = stringRedisTemplate.getConnectionFactory().getConnection(); cc.subscribe(new MessageListener() { @Override public void onMessage(Message message, byte[] pattern) { byte[] body = message.getBody(); System.out.println(new String(body)); } }, "ooxx".getBytes()); while(true){ stringRedisTemplate.convertAndSend("ooxx","hello from wo zi ji "); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }