使用redisTemplete簡單封裝的一個通用操作類
阿新 • • 發佈:2019-01-25
import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; importjava.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Set; /** * Redis操作類 */ public class RedisManager { /** * 過期時間,預設30分鐘 */ private Long expireTime = 1000 * 60 * 30L; /** * Redis模版 */ private RedisTemplate<Serializable, Serializable> redisTemplate; public void setRedisTemplate(RedisTemplate<Serializable, Serializable> redisTemplate) { this.redisTemplate = redisTemplate; } public void setExpireTime(long expireTime) { this.expireTime = expireTime; } private RedisSerializer<Object> getKeySerializer() { return(RedisSerializer<Object>) redisTemplate.getKeySerializer(); } private RedisSerializer<Object> getValueSerializer() { return (RedisSerializer<Object>) redisTemplate.getValueSerializer(); } private RedisSerializer<Object> getHashKeySerializer() { return (RedisSerializer<Object>) redisTemplate.getHashKeySerializer(); } private RedisSerializer<Object> getHashValueSerializer() { return (RedisSerializer<Object>) redisTemplate.getHashValueSerializer(); } private byte[] getKeyBytes(Object key) { return getKeySerializer().serialize(key); } private byte[] getHashKeyBytes(Object key) { return getHashKeySerializer().serialize(key); } private byte[] getValueBytes(Object value) { return getValueSerializer().serialize(value); } private byte[] getHashValueBytes(Object value) { return getHashValueSerializer().serialize(value); } private <T> T deserializeValue(byte[] bytes) { return (T) getValueSerializer().deserialize(bytes); } private <T> T deserializeHashValue(byte[] bytes) { return (T) getHashValueSerializer().deserialize(bytes); } /** * 新增快取資料(給定key已存在,進行覆蓋) * * @param key * @param value * @throws DataAccessException */ public void set(final Object key, final Object value) throws DataAccessException { redisTemplate.execute(new RedisCallback<Void>() { @Override public Void doInRedis(RedisConnection connection) throws DataAccessException { connection.set(getKeyBytes(key), getValueBytes(value)); return null; } }); } /** * 新增快取資料(給定key已存在,不進行覆蓋,直接返回false) * * @param key * @param value * @return 操作成功返回true,否則返回false * @throws DataAccessException */ public boolean setNX(final Object key, final Object value) throws DataAccessException { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(getKeyBytes(key), getValueBytes(value)); } }); return result; } /** * 新增快取資料,設定快取失效時間 * * @param key * @param value * @param expireSeconds 過期時間,單位 秒 * @throws DataAccessException */ public void setEx(final Object key, final Object value, final long expireSeconds) throws DataAccessException { redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(getKeyBytes(key), expireSeconds, getValueBytes(value)); return true; } }); } /** * 新增快取資料,設定預設快取失效時間 * * @param key * @param value * @throws DataAccessException */ public void setEx(Object key, Object value) throws DataAccessException { setEx(key, value, expireTime); } /** * 獲取key對應value * * @param key * @return * @throws DataAccessException */ public <T> T get(final Object key) throws DataAccessException { byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(getKeyBytes(key)); } }); if (result == null) { return null; } return deserializeValue(result); } public boolean hSet(final Object key, final Object field, final Object value) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.hSet(getKeyBytes(key), getHashKeyBytes(field), getHashValueBytes(value)); } }); return result; } public <T> T hGet(final Object key, final Object field) throws DataAccessException { byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.hGet(getKeyBytes(key), getHashKeyBytes(field)); } }); if (result == null) { return null; } return deserializeHashValue(result); } /** * 設定超時時間 * * @param key * @param seconds * @return */ public boolean expire(final Object key, final long seconds) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.expire(getKeyBytes(key), seconds); } }); return result; } /** * 刪除指定key資料 * * @param key * @return 返回操作影響記錄數 */ public long del(final Object key) { if (key == null) { return 0l; } Long delNum = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { return connection.del(getKeyBytes(key)); } }); return delNum; } /** * 用於刪除雜湊表 key 中的一個或多個指定欄位,不存在的欄位將被忽略 * * @param key 雜湊表對應的鍵 * @param filed 雜湊表中要刪除的鍵 * @return */ public Long hdel(final Object key, final Object... filed) { if (key == null) { return 0l; } final byte[] keys = getKeyBytes(key); if (keys == null || keys.length == 0) { return 0L; } Long delNum = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection connection) throws DataAccessException { List<byte[]> fieldList = new ArrayList<byte[]>(); for (Object f : filed) { if (f != null) { byte[] hfs = getHashKeyBytes(f); if (hfs != null && hfs.length > 0) { fieldList.add(hfs); } } } if (fieldList.size() == 0) { return 0L; } byte[][] bytes = new byte[0][]; return connection.hDel(keys, fieldList.toArray(bytes)); } }); return delNum; } public Set<byte[]> keys(final Object key) { if (key == null) { return null; } Set<byte[]> bytesSet = redisTemplate.execute(new RedisCallback<Set<byte[]>>() { @Override public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException { return connection.keys(getKeyBytes(key)); } }); return bytesSet; } public void flushDb() { redisTemplate.execute(new RedisCallback<Void>() { @Override public Void doInRedis(RedisConnection redisConnection) throws DataAccessException { redisConnection.flushDb(); return null; } }); } /** * size */ public Long dbSize() { Long dbSize = redisTemplate.execute(new RedisCallback<Long>() { @Override public Long doInRedis(RedisConnection redisConnection) throws DataAccessException { return redisConnection.dbSize(); } }); return dbSize; } }