这里有新鲜出炉的 Redis 命令参考,程序狗速度看过来!
Redis Key-Value 数据库
Redis 是一个开源的使用 ANSI C 语言编写,支持网络,可基于内存亦可持久化的日志型,Key-Value 数据库,并提供多种语言的 API.
本篇文章主要介绍了 Java 自定义注解实现 Redis 自动缓存的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下.
在实际开发中,可能经常会有这样的需要:从 MySQL 中查询一条数据(比如用户信息),此时需要将用户信息保存至 Redis.
刚开始我们可能会在查询的业务逻辑之后再写一段 Redis 相关操作的代码,时间长了后发现这部分代码实际上仅仅做了 Redis 的写入动作,跟业务逻辑没有实质的联系,那么有没有什么方法能让我们省略这些重复劳动呢?
首先想到用 AOP,在查询到某些数据这一切入点(Pointcut)完成我们的切面相关处理(也就是写入 Redis).那么,如何知道什么地方需要进行缓存呢,也就是什么地方需要用到 AOP 呢?参考数据库事务的实现用到了 @Transactional,那我们也可以自定义一个注解 @RedisCache,将此注解用在需要的方法上,方法的返回结果作为需要保存的信息,方法的查询参数(比如用户的 id)可以用来作为 key.
上面的分析考虑下来貌似可行,那么接下来就动手实践吧!
详细步骤
1. 创建一个自定义注解 @RedisCache
2. 创建缓存写入的辅助类:RedisHelper.java,其中包含一个范型方法用于接收不同类的实例对象,以保证我们的方法能够通用.这里比较简单,直接把对象转成 json,在 Redis 中用 string 保存.而且不管什么情况统统写入,实际还可以完善下具体逻辑,比如判断缓存是否已存在,缓存信息是否最新等等.
package redis;
import java.lang.annotation.*;
/**
* 自定义注解,结合 AOP 实现 Redis 自动缓存
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@Documented
public @interface RedisCache {
}
3. 创建 RedisCacheAspect.java,利用 AOP 框架 AspectJ 完成切面处理(用万金油环绕通知吧,按需要有取舍地使用具体某些类型的通知吧),我们这里用到了返回通知,也就是方法调用成功得到返回结果后进行切面处理动作
package redis;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisHelper {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public <T> void saveCache(String key,T t){
String json = JSONObject.toJSONString(t);
stringRedisTemplate.opsForValue().set(key,json);
}
}
4. 接下来是具体业务相关的代码
package redis;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class RedisCacheAspect {
@Autowired
private RedisHelper redisHelper;
@Pointcut("@annotation(redis.RedisCache)")
public void setJoinPoint(){}
// 环绕通知: 可以获取返回值
@Around(value = "setJoinPoint()")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
Object result = null;
try {
// 前置通知
result = proceedingJoinPoint.proceed();
// 返回通知
// 缓存至 Redis
Object[] args = proceedingJoinPoint.getArgs();
//key 策略:需要缓存的对象的全类名 - id,如:entity.User-1
redisHelper.saveCache(result.getClass().getName()+"-"+args[0],result);
} catch (Throwable e) {
// 异常通知
}
// 后置通知
return result;
}
}
UserService.java,其中 get 方法上使用了自定义注解 @RedisCache
UserController.java
package controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
@SuppressWarnings("unused")
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET,produces = "application/json;charset=utf-8")
@ResponseBody
public String test(@PathVariable Long id){
return JSONObject.toJSONString(userService.get(id));
}
}
5. 测试
package service;
import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.RedisCache;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserService<User> implements BaseService<User> {
@Autowired
private UserDao userDao;
public Map add(User user) {
return null;
}
public Map update(User user) {
return null;
}
@RedisCache
public User get(Long id) {
return (User) userDao.get(id);
}
public List<User> query(User user) {
List<User> list = new ArrayList<User>();
list = userDao.query(user);
return list;
}
public Map delete(User user) {
return null;
}
}
浏览器直接访问 http://localhost:8080/user/1,得到返回结果
http://localhost:8080/user/1
连接 Redis 查看结果
好了,到此我们已经看到开头的想法验证成功了,只需要在查询的方法上加上注解 @RedisCache,就自动地悄无声息地写入 Redis 了,是不是很方便!
127.0.0.1:6381> keys entity*
1) "entity.User-1"
127.0.0.1:6381> get entity.User-1
"{\"id\":1,\"mobile\":\"110\",\"name\":\"\xe7\x94\xa8\xe6\x88\xb71\",\"password\":\"123456\",\"username\":\"0001\"}"
127.0.0.1:6381>
来源: http://www.phperz.com/article/18/0128/357461.html