package com.yeshi.buwan.util; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.google.gson.Gson; import com.yeshi.buwan.vo.video.VideoListResultVO; import org.yeshi.utils.SerializeUtil; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.params.SetParams; //抢红包采用的redis @Component public class RedisManager { @Resource private JedisPool jedisPool; public Jedis getJedis() { Jedis jedis = jedisPool.getResource(); return jedis; } /** * 缓存字符串 * * @param key * @param value */ private void setString(String key, String value) { Jedis jedis = getJedis(); SetParams params = new SetParams().nx().ex(60); jedis.set(key, value, params); try { jedis.set(key, value); } finally { jedis.close(); } } /** * 删除某个键值 * * @param key * @param value */ private void removeKey(String key) { Jedis jedis = getJedis(); try { jedis.del(key); } finally { jedis.close(); } } /** * 缓存字符串 * * @param key * @param value * @param seconds -缓存时间(s) */ private void setString(String key, String value, int seconds) { Jedis jedis = getJedis(); try { jedis.setex(key, seconds, value); } finally { jedis.close(); } } private String getString(String key) { Jedis jedis = getJedis(); try { return jedis.get(key); } finally { jedis.close(); } } public void increase(String key) { Jedis jedis = getJedis(); try { jedis.incr(key); } finally { jedis.close(); } } public void increase(String key, int s) { Jedis jedis = getJedis(); try { jedis.incr(key); jedis.expire(key, s); } finally { jedis.close(); } } public void expire(String key, int seconds) { Jedis jedis = getJedis(); try { jedis.expire(key, seconds); } finally { jedis.close(); } } public void cacheCommonString(String key, String value, int seconds) { setString(key, value, seconds); } public void cacheCommonString(String key, String value) { setString(key, value); } public String getCommonString(String key) { return getString(key); } public void removeCommonString(String key) { removeKey(key); } public void remove(String key) { removeKey(key); } /** * 保存视频数据 * * @param key * @param videoData */ public void saveVideoList(String key, VideoListResultVO videoData) { Gson gson = new Gson(); cacheCommonString(key, gson.toJson(videoData), 60 * 60 * 8); } /** * 获取视频列表 * * @param key * @return */ public VideoListResultVO getVideoList(String key) { String st = getCommonString(key); if (!StringUtil.isNullOrEmpty(st)) { return new Gson().fromJson(st, VideoListResultVO.class); } return null; } /** * 保存列表 * * @param clazzList * @param key * @param seconds */ public T saveObjList(List clazzList, String key, Integer seconds) { if (clazzList == null) return null; String value = new Gson().toJson(clazzList); if (seconds != null) cacheCommonString(key, value, seconds); else cacheCommonString(key, value); return null; } /** * 获取对象 * * @param clazz * @param key * @return */ public Class getObj(Class clazz, String key) { String value = getCommonString(key); return (Class) new Gson().fromJson(value, clazz); } /** * 获取列表 * * @param clazz * @param key * @return */ public List getObjList(Class clazz, String key) { String value = getCommonString(key); return JsonUtil.jsonToList(value, clazz); } public boolean isSmsFrequencyLimit(String phone, Integer type) { if (type == null) { type = 0; } String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type); String value = getCommonString(key); if (StringUtil.isNullOrEmpty(value)) return false; else return true; } /** * 从redis缓存中查询,反序列化 * * @param redisKey * @return */ public Object getDataFromRedis(String redisKey) { //查询 Jedis jedis = jedisPool.getResource(); try { byte[] result = jedis.get(redisKey.getBytes()); //如果查询没有为空 if (null == result) { return null; } //查询到了,反序列化 return SerializeUtil.unSerialize(result); } finally { jedis.close(); } } /** * 将数据库中查询到的数据放入redis * * @param redisKey * @param obj */ public void setDataToRedis(String redisKey, Object obj, Integer seconds) { //序列化 byte[] bytes = SerializeUtil.serialize(obj); SetParams params = null; if (seconds != null) { params = new SetParams().nx().ex(seconds); } //存入redis Jedis jedis = jedisPool.getResource(); try { jedis.set(redisKey.getBytes(), bytes, params); } finally { jedis.close(); } } }