package redis;
import java.lang.annotation.*;
/**
* 自定义注解,结合AOP实现Redis自动缓存
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@Documented
public @interface RedisCache {
}
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);
}
}
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;
}
}
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));
}
}
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;
}
}
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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有