2018-12-08 通用缓存key封装
2018-12-08 本文已影响0人
培根好吃
1.通用缓存key封装
利用模板模式 接口-> 抽象类->实现类
service层
package com.ryan.miaosha.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ryan.miaosha.dao.User;
import com.ryan.miaosha.domain.CodeMsg;
import com.ryan.miaosha.domain.Result;
import com.ryan.miaosha.redis.RedisService;
import com.ryan.miaosha.redis.UserKey;
import com.ryan.miaosha.service.UserService;
//1. rest api json数据 2. 页面
@Controller
@RequestMapping("/demo")
public class DemoController {
@Autowired
UserService userService;
@Autowired
RedisService redisService;
//请求成功
@RequestMapping("/redis/get")
@ResponseBody
Result<User> getRedis() {
User user1 = redisService.get(UserKey.getById, ""+1,User.class);
return Result.success(user1);
}
//请求成功
@RequestMapping("/redis/set")
@ResponseBody
Result<Boolean> setRedis() {
User user=new User();
user.setId(1);
user.setName("ryan");
redisService.set(UserKey.getById, ""+1, user);
return Result.success(true);
}
@RequestMapping("/")
@ResponseBody
String home() {
return "hello springboot!";
}
//请求成功
@RequestMapping("/dao")
@ResponseBody
Result<User> selectById() {
User idUser = userService.getById(1);
return Result.success(idUser);
}
//请求异常
@RequestMapping("/helloError")
@ResponseBody
Result<String> helloError() {
return Result.error(CodeMsg.SERVER_ERROR);
}
@RequestMapping("/thymeleaf")
String thymeleaf(Model model) {
model.addAttribute("name","ryan");
return "ryan";
}
}
redis层
package com.ryan.miaosha.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class RedisService {
@Autowired
JedisPool jedisPool;
//从redis中获取数据 ,将json数据转变为对象
public <T> T get(KeyPrefix keyPrefix,String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realKey=keyPrefix.getPrefix()+key;
String string = jedis.get(realKey);
T t=stringToBean(string,clazz);
return t;
} finally {
returnToPool(jedis);}
}
private <T> String beanToString(T value) {
if(value==null)
return null;
Class<?> clazz=value.getClass();
if(clazz==int.class||clazz==Integer.class) {
return ""+value;
}else if (clazz==String.class) {
return (String)value;
}else if(clazz==long.class||clazz==Long.class) {
return ""+value;}else {
return JSON.toJSONString(value);
}
}
//redis存入数据 ,将对象转变为json数据,再存入redis中
public <T> Boolean set(KeyPrefix keyPrefix,String key, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str=beanToString(value);
if(str==null||str.length()<=0) {
return false;
}
String realKey=keyPrefix.getPrefix()+key;
int expireTime = keyPrefix.expireTime();
if(expireTime<=0) {
jedis.set(realKey, str);
}else {
jedis.setex(key, expireTime, str);
}
return true;
} finally {
returnToPool(jedis);
}}
@SuppressWarnings("unchecked")
private <T> T stringToBean(String str,Class<T> clazz) {
if(str==null||str.length()<=0||clazz==null) {
return null;
}
if(clazz==int.class||clazz==Integer.class) {
return (T) Integer.valueOf(str);
}else if (clazz==String.class) {
return (T) str;
}else if(clazz==long.class||clazz==Long.class) { return (T) Long.valueOf(str);}else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
private void returnToPool(Jedis jedis) {
if(jedis!=null)
jedis.close(); //将jedis返回连接池
}
//判断一个key是否存在
public <T> Boolean exists(KeyPrefix keyPrefix,String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realKey=keyPrefix.getPrefix()+key;
return jedis.exists(realKey);
} finally {
returnToPool(jedis);
}
}
// 自增
public <T> Long incr(KeyPrefix keyPrefix,String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realKey=keyPrefix.getPrefix()+key;
return jedis.incr(realKey);
} finally {
returnToPool(jedis);
}
}
// 自减
public <T> Long decr(KeyPrefix keyPrefix,String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String realKey=keyPrefix.getPrefix()+key;
return jedis.decr(realKey);
} finally {
returnToPool(jedis);
}
}
}
前缀 KeyPrefix层
public interface KeyPrefix {
public int expireTime();//过期时间 秒
public String getPrefix();
}
public abstract class BasePrefix implements KeyPrefix{
private int expiredTime;
private String prefix;
public BasePrefix(String prefix) {
this(0, prefix);
// TODO Auto-generated constructor stub
}
public BasePrefix(int expiredTime, String prefix) {
super();
this.expiredTime = expiredTime;
this.prefix = prefix;
}
public int expiredTime() { //默认0代表永不过期
return expiredTime;
}
public String getPrefix() {
String ClassName = getClass().getSimpleName();
return ClassName+":"+prefix;
}
}
public class UserKey extends BasePrefix{
private UserKey(String prefix) {
super( prefix);
// TODO Auto-generated constructor stub
}
@Override
public int expireTime() {
// TODO Auto-generated method stub
return 0;
}
public static UserKey getById=new UserKey("id");
public static UserKey getByName=new UserKey("name");
}