欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > redis 缓存使用

redis 缓存使用

2024/12/31 1:03:22 来源:https://blog.csdn.net/chengchong_cc/article/details/144568037  浏览:    关键词:redis 缓存使用

工具类

package org.springblade.questionnaire.redis;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import java.util.Map;
import java.util.concurrent.TimeUnit;@Service
public class RedisService {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private final ObjectMapper objectMapper = new ObjectMapper();// 存储对象到Redispublic void setObjectToRedis(String key, Object obj) {try {// 将对象序列化为JSON字符串String value = objectMapper.writeValueAsString(obj);// 使用StringRedisTemplate将JSON字符串存储到RedisstringRedisTemplate.opsForValue().set(key, value);} catch (JsonProcessingException e) {throw new RuntimeException("Failed to serialize object to JSON", e);}}// 从Redis获取对象public <T> T getObjectFromRedis(String key, Class<T> clazz) {String json = stringRedisTemplate.opsForValue().get(key);if (json != null) {try {// 将JSON字符串反序列化为对象return objectMapper.readValue(json, clazz);} catch (Exception e) {throw new RuntimeException("Failed to deserialize JSON to object", e);}}return null;}// 存储对象数组到Redispublic <T> void setArrayToRedis(String key, T[] array) {try {// 将对象数组序列化为JSON字符串String value = objectMapper.writeValueAsString(array);// 使用StringRedisTemplate将JSON字符串存储到RedisstringRedisTemplate.opsForValue().set(key, value);} catch (JsonProcessingException e) {throw new RuntimeException("Failed to serialize object array to JSON", e);}}// 从Redis获取对象数组@SuppressWarnings("unchecked")public <T> T[] getArrayFromRedis(String key, Class<T[]> clazz) {String json = stringRedisTemplate.opsForValue().get(key);if (json != null) {try {// 将JSON字符串反序列化为对象数组return objectMapper.readValue(json, clazz);} catch (Exception e) {throw new RuntimeException("Failed to deserialize JSON to object array", e);}}return null;}// 存储对象到Redis并设置过期时间public void setObjectToRedisWithTime(String key, Object obj, long timeout, TimeUnit timeUnit) {try {// 将对象序列化为JSON字符串String value = objectMapper.writeValueAsString(obj);// 使用StringRedisTemplate将JSON字符串存储到Redis并设置过期时间stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);} catch (JsonProcessingException e) {throw new RuntimeException("Failed to serialize object to JSON", e);}}// 存储对象数组到Redis并设置过期时间public <T> void setArrayToRedisWithTime(String key, T[] array, long timeout, TimeUnit timeUnit) {try {// 将对象数组序列化为JSON字符串String value = objectMapper.writeValueAsString(array);// 使用StringRedisTemplate将JSON字符串存储到Redis并设置过期时间stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);} catch (JsonProcessingException e) {throw new RuntimeException("Failed to serialize object array to JSON", e);}}// 存储字符串到Redis并设置过期时间public void setStringToRedis(String key, String value, long timeout, TimeUnit timeUnit) {// 使用StringRedisTemplate将字符串存储到Redis并设置过期时间stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);}// 从Redis获取字符串public String getStringFromRedis(String key) {return stringRedisTemplate.opsForValue().get(key);}// 存储 Map<String, Map<String, String>> 到 Redis 并设置过期时间public void setTranslationsToRedis(String key, Map<String, Map<String, String>> translations, long timeout, TimeUnit timeUnit) {try {// 将 Map 序列化为 JSON 字符串String value = objectMapper.writeValueAsString(translations);// 使用 StringRedisTemplate 将 JSON 字符串存储到 Redis 并设置过期时间stringRedisTemplate.opsForValue().set(key, value, timeout, timeUnit);} catch (JsonProcessingException e) {throw new RuntimeException("Failed to serialize translations map to JSON", e);}}// 从 Redis 获取 Map<String, Map<String, String>>public Map<String, Map<String, String>> getTranslationsFromRedis(String key) {String json = stringRedisTemplate.opsForValue().get(key);if (json != null) {try {// 将 JSON 字符串反序列化为 Map<String, Map<String, String>>return objectMapper.readValue(json, new TypeReference<Map<String, Map<String, String>>>() {});} catch (Exception e) {throw new RuntimeException("Failed to deserialize JSON to translations map", e);}}return null;}
}

引用

	@Autowiredprivate RedisService redisService;

调用方法

一般设置过期时间一天,看情况而定

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com