// private JedisPool jedisPool = new JedisPool("redis.vip.idc.pplive.cn", 19670); @Test public void t2() throws Exception { for (; ; ) { redisTemplate.opsForHash().put("cache", "pptv", "0"); new ConcurrentEnvironment(5) { @Override protected void testCode() { /////// ----------------------- RedisReentrantLock lock = new RedisReentrantLock(jedisPool, "GET_RECENT_CARD_INFO_LOCK"); DefaultRedisMap<String, String> map = new DefaultRedisMap<String, String>("cache", redisTemplate); for (int i = 0; i < 1000; i++) { try { if (lock.tryLock(5000L, TimeUnit.MILLISECONDS)) { // 获取锁超时时间为5秒 int s = Integer.valueOf(map.get("pptv")); if (s == 3433) s = 1 / 0; map.put("pptv", ++s + ""); } else { // TODO 获得锁超时后要做的事 } } catch (Exception e) { map.put("pptv", 3434 + ""); } finally { lock.unlock(); } /////// ----------------------- } } }.start(); String s = (String) redisTemplate.opsForHash().get("cache", "pptv"); System.out.println("end! -> " + s); } }
public void TestRedis() { RedisTemplate<String, String> jedisTemplate = (RedisTemplate<String, String>) SpringUtils.getBean("jedisTemplate"); jedisTemplate.setValueSerializer(new StringRedisSerializer()); Long size = jedisTemplate.opsForList().size(MsgListName); if (0L == size) return; // // show the Hashmap 's value Map<Object, Object> hm = jedisTemplate.opsForHash().entries("Sender_Rules_HashMap"); for (Entry<Object, Object> entry : hm.entrySet()) { System.out.println(entry.getKey()); } for (int i = 0; i < 1; i++) { String json = jedisTemplate.opsForList().rightPop(MsgListName); AlarmMsgEntity entity = JsonUtil.getInstance().json2Obj(json, AlarmMsgEntity.class); // if has key then wait next time to alarm if (jedisTemplate.hasKey(entity.getRedisKey())) continue; System.out.println(entity.getRedisKey()); if (jedisTemplate.opsForHash().hasKey("Sender_Rules_HashMap", entity.getRedisKey())) { System.out.println("find one ................."); AlarmSenderEntity ae = (AlarmSenderEntity) jedisTemplate.opsForHash().get("Sender_Rules_HashMap", entity.getRedisKey()); if (ae.getSendType().equals("短信")) { System.out.println("sends messages for alarm!"); jedisTemplate.expire(entity.getRedisKey(), 5, TimeUnit.MINUTES); } } } }
/** @param redisConnectionFactory */ public RedisAggregateCounterRepository(RedisConnectionFactory redisConnectionFactory) { super("aggregatecounters", redisConnectionFactory); RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class)); redisTemplate.afterPropertiesSet(); hashOperations = redisTemplate.opsForHash(); setOperations = redisTemplate.opsForSet(); }
public void putAllProp(Map<String, Object> properties, String key) { redisTemplate.opsForHash().putAll(key, properties); }
public Object getObjectByKey(String serial_key, String key) { if (redisTemplate.opsForHash().hasKey(serial_key, key)) { return redisTemplate.opsForHash().get(serial_key, key); } return null; }
public void delObjectByKey(String serial_key, String key) { if (redisTemplate.opsForHash().hasKey(serial_key, key)) { redisTemplate.opsForHash().delete(serial_key, key); } }
public void putObjectByKey(String serial_key, String key, Object value) { redisTemplate.opsForHash().put(serial_key, key, value); }
public boolean hsetnx(String key, Object field, String value) { hashOps = redisTemplate.opsForHash(); return hashOps.putIfAbsent(key, field, value); }
/** * Hash数据类型 读取 key绑定的hash集合的keys * * @param key * @return set集合 */ public Set getHashKeys(String key) { hashOps = redisTemplate.opsForHash(); return (Set) hashOps.keys(key); }
/** * Hash数据类型 读取 key绑定的hash集合 * * @param key * @return map集合 key-value */ public Map<Object, Object> getHash(String key) { hashOps = redisTemplate.opsForHash(); return hashOps.entries(key); }
/** * Hash数据类型 查找 key绑定的hash集合的大小 * * @param key * @return */ public long getHashSize(String key) { hashOps = redisTemplate.opsForHash(); return hashOps.size(key); }
/** * Hash数据类型 查找 key绑定的hash集合中hashKey的值 * * @param key * @param hashKey * @return */ public Object getHashValue(String key, Object hashKey) { hashOps = redisTemplate.opsForHash(); return hashOps.get(key, hashKey); }
/** * Hash数据类型 查找 key绑定的hash集合中hashKey 的元素是否存在 * * @param key * @param hashKey * @return true存在 false不存在 */ public boolean hasHashKey(String key, Object hashKey) { hashOps = redisTemplate.opsForHash(); return hashOps.hasKey(key, hashKey); }
/** * Hash数据类型 批量 key绑定的hash集合新增元素 * * @param key * @param map */ public void putHash(String key, Map<Object, Object> map) { hashOps = redisTemplate.opsForHash(); hashOps.putAll(key, map); }
/** * Hash数据类型 key绑定的hash集合新增元素 * * @param key * @param hashKey * @param hashValue */ public void putHash(String key, Object hashKey, Object hashValue) { hashOps = redisTemplate.opsForHash(); hashOps.put(key, hashKey, hashValue); }
public Object getHashProp(String key, String val) { return redisTemplate.opsForHash().get(key, val); }
public void hincreby(String key, Object field, Integer delta) { hashOps = redisTemplate.opsForHash(); hashOps.increment(key, field, delta); }
public boolean hexists(String key, Object field) { hashOps = redisTemplate.opsForHash(); return hashOps.hasKey(key, field); }
@PostConstruct public void init() { operations = template.opsForValue(); hashOperations = template.opsForHash(); }
@Override public HashOperations getHashOper() { return template.opsForHash(); }