首页>代码>spring boot+thymeleaf+bootstrap实现文件文本短地址服务>/ftshorter/src/main/java/com/yicheng/cache/RedisCache.java
package com.yicheng.cache; import com.google.gson.Gson; import com.yicheng.utils.ProtoStuffSerializerUtil; import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Component; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; /** * redis缓存 * * @author luo.yicheng * */ @Component public class RedisCache { public final static String CAHCENAME = "cache";// 缓存名 public final static int CAHCETIME = 60;// 默认缓存时间 @Autowired private RedisTemplate<String, String> redisTemplate; public <T> boolean putCache(String key, T obj) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public <T> void putCacheWithExpireTime(String key, T obj, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); } public <T> boolean putListCache(String key, List<T> objList) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public <T> boolean putListCacheWithExpireTime(String key, List<T> objList, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); return result; } public <T> T getCache(final String key, Class<T> targetClass) { byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserialize(result, targetClass); } public <T> List<T> getListCache(final String key, Class<T> targetClass) { byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserializeList(result, targetClass); } /** * 精确删除key * * @param key */ public void deleteCache(String key) { redisTemplate.delete(key); } /** * 模糊删除key * * @param pattern */ public void deleteCacheWithPattern(String pattern) { Set<String> keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } /** * 清空所有缓存 */ public void clearCache() { deleteCacheWithPattern(RedisCache.CAHCENAME + "|*"); } /** * 将value对象以json格式写入缓存 * * @param key * @param value * @param time * 失效时间(秒) */ public void setJsonWithExpireTime(String key, Object value, long time) { redisTemplate.opsForValue().set(key, new Gson().toJson(value)); if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } } /** * 获取缓存json对象<br> * * @param key * key * @param clazz * 类型 * @return */ public <T> T getJson(String key, Class<T> clazz) { return new Gson().fromJson(redisTemplate.boundValueOps(key).get(), clazz); } }

我真的是小明 LV10
2023年10月17日
疯子庭 LV8
2021年11月30日
Everythone LV2
2019年10月11日
china_0221 LV41
2019年8月30日
RobinOOOooo LV6
2019年8月26日
LHJ123 LV30
2019年8月8日
低调人 LV38
2019年7月9日
Sancly LV1
2019年7月9日
超超哥哥 LV16
2019年6月25日
yandong LV12
2019年6月19日

f22m1a2b2 LV17
2024年5月31日
微信网友_6927932988952576 LV12
2024年3月31日
微信网友_5986558820093952 LV4
2023年12月28日
WBelong LV8
2023年12月18日
我真的是小明 LV10
2023年10月17日
pengwink LV2
2022年12月2日
MingZheLi LV3
2022年8月29日
wjh12345654321 LV14
2022年8月25日
mwh1001 LV15
2022年4月25日
crosa_Don LV18
2022年4月1日