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 {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	private final ObjectMapper objectMapper = new ObjectMapper();

	// 存储对象到Redis
	public void setObjectToRedis(String key, Object obj) {
		try {
			// 将对象序列化为JSON字符串
			String value = objectMapper.writeValueAsString(obj);
			// 使用StringRedisTemplate将JSON字符串存储到Redis
			stringRedisTemplate.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;
	}

	// 存储对象数组到Redis
	public <T> void setArrayToRedis(String key, T[] array) {
		try {
			// 将对象数组序列化为JSON字符串
			String value = objectMapper.writeValueAsString(array);
			// 使用StringRedisTemplate将JSON字符串存储到Redis
			stringRedisTemplate.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;
	}
}

引用

	@Autowired
	private RedisService redisService;

调用方法

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

相关推荐
无限大.3 小时前
前端知识速记:节流与防抖
前端
十八朵郁金香3 小时前
【VUE案例练习】前端vue2+element-ui,后端nodo+express实现‘‘文件上传/删除‘‘功能
前端·javascript·vue.js
学问小小谢3 小时前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
LCG元4 小时前
Vue.js组件开发-实现全屏图片文字缩放切换特效
前端·javascript·vue.js
还是鼠鼠5 小时前
图书管理系统 Axios 源码__新增图书
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
Linux运维老纪8 小时前
DNS缓存详解(DNS Cache Detailed Explanation)
计算机网络·缓存·云原生·容器·kubernetes·云计算·运维开发
还是鼠鼠8 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
轻口味8 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
@_@哆啦A梦9 小时前
Redis 基础命令
java·数据库·redis
m0_zj9 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css