Redis实战-详细配置-优雅的使用Redis注解/RedisTemplate (3)

我们为了防止redis中key的重复,尽量会给不同的数据主体加上不同的前缀,这样我们在查看和统计的时候也方便操作。

/** * 缓存key 常量类 * @author ludangxin * @date 2021/8/11 */ public interface CacheConstant { /** * 用户cache name */ String USER_CACHE_NAME = "user_cache"; /** * 用户信息缓存key前缀 */ String USER_CACHE_KEY_PREFIX = "user_"; /** * 角色cache name */ String ROLE_CACHE_NAME = "role_cache"; /** * 角色信息缓存key前缀 */ String ROLE_CACHE_KEY_PREFIX = "role_"; /** * 获取角色cache key * @param suffix 后缀 * @return key */ static String getRoleCacheKey(String suffix) { return ROLE_CACHE_NAME + "::" + ROLE_CACHE_KEY_PREFIX + suffix; } } 2.4.2 RedisUtil import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; import java.util.*; import java.util.concurrent.TimeUnit; /** * spring redis 工具类 * @author ludangxin **/ @Component @RequiredArgsConstructor @SuppressWarnings(value = { "unchecked", "rawtypes" }) public class RedisUtil { public final RedisTemplate redisTemplate; /** * 缓存基本的对象,Integer、String、实体类等 * @param key 缓存的键值 * @param value 缓存的值 * @return 缓存的对象 */ public <T> ValueOperations<String, T> setCacheObject(String key, T value) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value); return operation; } /** * 缓存基本的对象,Integer、String、实体类等 * @param key 缓存的键值 * @param value 缓存的值 * @param timeout 时间 * @param timeUnit 时间颗粒度 * @return 缓存的对象 */ public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value, timeout, timeUnit); return operation; } /** * 获得缓存的基本对象。 * @param key 缓存键值 * @return 缓存键值对应的数据 */ public <T> T getCacheObject(String key) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); return operation.get(key); } /** * 删除单个对象 * @param key */ public void deleteObject(String key) { redisTemplate.delete(key); } /** * 删除集合对象 * @param collection */ public void deleteObject(Collection collection) { redisTemplate.delete(collection); } /** * 缓存List数据 * @param key 缓存的键值 * @param dataList 待缓存的List数据 * @return 缓存的对象 */ public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) { ListOperations listOperation = redisTemplate.opsForList(); if (null != dataList) { int size = dataList.size(); for (int i = 0; i < size; i++) { listOperation.leftPush(key, dataList.get(i)); } } return listOperation; } /** * 获得缓存的list对象 * @param key 缓存的键值 * @return 缓存键值对应的数据 */ public <T> List<T> getCacheList(String key) { List<T> dataList = new ArrayList<T>(); ListOperations<String, T> listOperation = redisTemplate.opsForList(); Long size = listOperation.size(key); for (int i = 0; i < size; i++) { dataList.add(listOperation.index(key, i)); } return dataList; } /** * 缓存Set * @param key 缓存键值 * @param dataSet 缓存的数据 * @return 缓存数据的对象 */ public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) { BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); Iterator<T> it = dataSet.iterator(); while (it.hasNext()) { setOperation.add(it.next()); } return setOperation; } /** * 获得缓存的set * @param key * @return */ public <T> Set<T> getCacheSet(String key) { Set<T> dataSet = new HashSet<T>(); BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key); dataSet = operation.members(); return dataSet; } /** * 缓存Map * @param key * @param dataMap * @return */ public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) { HashOperations hashOperations = redisTemplate.opsForHash(); if (null != dataMap) { for (Map.Entry<String, T> entry : dataMap.entrySet()) { hashOperations.put(key, entry.getKey(), entry.getValue()); } } return hashOperations; } /** * 获得缓存的Map * @param key * @return */ public <T> Map<String, T> getCacheMap(String key) { Map<String, T> map = redisTemplate.opsForHash().entries(key); return map; } /** * 获得缓存的基本对象列表 * @param pattern 字符串前缀 * @return 对象列表 */ public Collection<String> keys(String pattern) { return redisTemplate.keys(pattern); } } 2.5 controller 2.5.1 UserController import com.ldx.redis.entity.SysUser; import com.ldx.redis.service.SysUserService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 用户管理 * @author ludangxin * @date 2021/8/11 */ @RestController @RequestMapping("user") @RequiredArgsConstructor public class UserController { private final SysUserService userService; @GetMapping public List<SysUser> queryAll() { return userService.queryAll(); } @GetMapping("{userId}") public SysUser getUserInfo(@PathVariable Long userId) { return userService.getUserInfo(userId); } @PostMapping public String add(@RequestBody SysUser user) { userService.add(user); return "新增成功~"; } @PutMapping("{userId}") public String update(@PathVariable Long userId, @RequestBody SysUser user) { userService.update(userId, user); return "更新成功~"; } @DeleteMapping("{userId}") public String del(@PathVariable Long userId) { userService.delete(userId); return "删除成功~"; } } 2.5.2 RoleController import com.ldx.redis.entity.SysRole; import com.ldx.redis.service.SysRoleService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 角色管理 * @author ludangxin * @date 2021/8/12 */ @RestController @RequestMapping("role") @RequiredArgsConstructor public class RoleController { private final SysRoleService roleService; @GetMapping public List<SysRole> queryAll() { return roleService.queryAll(); } @GetMapping("{roleId}") public SysRole getUserInfo(@PathVariable Long roleId) { return roleService.getRoleInfo(roleId); } @PostMapping public String add(@RequestBody SysRole role) { roleService.add(role); return "新增成功~"; } @PutMapping("{roleId}") public String update(@PathVariable Long roleId, @RequestBody SysRole role) { roleService.update(roleId, role); return "更新成功~"; } @DeleteMapping("{roleId}") public String del(@PathVariable Long roleId) { roleService.delete(roleId); return "删除成功~"; } } 2.6 service.impl 2.6.1 UserServiceImpl

优雅的使用redis注解实现对数据的缓存

@Cacheable:unless:当unless成立时则不缓存。这里判断size主要是不想将空值存入redis。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxfyy.html