1. 程式人生 > >redis工具類util

redis工具類util

import com.sdoson.util.spring.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.Serializable;
import java.util.ArrayList; import java.util.List; /** * redis工具 * 物件操作採用該類即可 * * @author 高宇飛 */ //@Repository public class RedisUtil { @SuppressWarnings("unchecked") private static RedisTemplate<Serializable, Serializable> redisTemplate = (RedisTemplate<Serializable, Serializable>) SpringContextHolder.getBean
("redisTemplate"); private static RedisTemplate redisTemplate2 = (RedisTemplate) SpringContextHolder.getBean("redisTemplate"); private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class); public static void set(final String key, Object value) { final byte[] vbytes = SerializeUtil.serialize
(value); try { redisTemplate.execute((RedisCallback<Object>) connection -> { byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key); connection.set(keyBytes, vbytes); return null; }); } catch (Exception e) { e.printStackTrace(); LOG.error(e.getMessage()); } } // -----------------List資料型別操作 start------------------ public static void setListValue(final String key, String value) { ListOperations<String, String> listOperations = redisTemplate2.opsForList(); listOperations.leftPush(key, value); } public static void setList(final String key, List<String> value) { ListOperations<String, String> listOperations = redisTemplate2.opsForList(); for (String aValue : value) { listOperations.leftPush(key, aValue); } } public static boolean getListSize(final String key) { ListOperations<String, String> listOperations = redisTemplate2.opsForList(); Long size = listOperations.size(key); return size > 0; } public static List<String> getList(final String key) { ListOperations<String, String> listOperations = redisTemplate2.opsForList(); Long size = listOperations.size(key); List<String> list = new ArrayList<>(); for (int i = 0; i < size; i++) { String obj = listOperations.rightPop(key); list.add(obj); } return list; } // -----------------List資料型別操作 end------------------ public static void set(final String key, Object value, final long l) { final byte[] vbytes = SerializeUtil.serialize(value); redisTemplate.execute((RedisCallback<Object>) connection -> { connection.setEx(redisTemplate.getStringSerializer().serialize(key), l, vbytes); return null; }); } public static <T> T get(final String key) { return redisTemplate.execute((RedisCallback<T>) connection -> { byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key); if (connection.exists(keyBytes)) { byte[] valueBytes = connection.get(keyBytes); @SuppressWarnings("unchecked") T value = (T) SerializeUtil.unserialize(valueBytes); return value; } return null; }); } public static void del(final String key) { final byte[] keyBytes = redisTemplate.getStringSerializer().serialize(key); redisTemplate.execute((RedisCallback<Object>) connection -> { connection.del(keyBytes); return null; }); } public static <T> T get(final String key, Class<T> elementType) { return redisTemplate.execute((RedisCallback<T>) connection -> { byte[] keybytes = redisTemplate.getStringSerializer().serialize(key); if (connection.exists(keybytes)) { byte[] valuebytes = connection.get(keybytes); @SuppressWarnings("unchecked") T value = (T) SerializeUtil.unserialize(valuebytes); return value; } return null; }); } /** * 判斷是否存在 * * @param key key * @return 存在/不存在 */ public static boolean contain(final String key) { return get(key) != null; } ///清空所有 public static void flushDb() { redisTemplate.execute((RedisCallback<Object>) connection -> { connection.flushDb(); return null; }); } }