0
点赞
收藏
分享

微信扫一扫

关于用redis缓存对象

独孤凌雪 2022-08-18 阅读 88


本文基本上是<<Java秒杀系统方案优化 高性能高并发实战>>的学习笔记

我们知道redis可以存储的数据结构比memcached多,它包含hash,list等等

但是如果,我有一个Person对象,里面两个变量,username,password,我想保存一个Person对象呢?
那很简单呀,要么使用pb把它序列化,要么把对象转成json字符串
从速度上讲pb要比json快
但是pb的结果是不可读的,相比较于json,对程序员不是那么友好
所以一般情况下,都是使用json的

package com.imooc.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;

/**
* 获取但个对象
* */
public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = prefix.getPrefix() + key;
String str = jedis.get(realKey);
T t = stringToBean(str, clazz);
return t;
}finally {
returnToPool(jedis);
}
}

/**
* 设置对象
* */
public <T> boolean set(KeyPrefix prefix, String key, T value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if(str == null || str.length() <= 0) {
return false;
}
//生成真正的key
String realKey = prefix.getPrefix() + key;
int seconds = prefix.expireSeconds();
if(seconds <= 0) {
jedis.set(realKey, str);
}else {
jedis.setex(realKey, seconds, str);
}
return true;
}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);
}
}

@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();
}
}

}

看到上面的代码,朋友们可能要问那个KeyPrefix是个什么东西
假如有个用户,它的key是abc
假如有个订单,它的key也是abc
而且,即使同一个用户,我也希望他在缓存里有两个项,一个是按照用户名去查,一个是根据电话号码去查
明白了吧?
我们保证每个模块内的数据的前缀都是不一样的,而且同一个数据也可以保存两次
那具体怎么实现呢?

package com.imooc.miaosha.redis;

public interface KeyPrefix {
public String getPrefix();
}

package com.imooc.miaosha.redis;
public abstract class BasePrefix implements KeyPrefix{
private String prefix;

public BasePrefix( String prefix) {
this.prefix = prefix;
}

public String getPrefix() {
String className = getClass().getSimpleName();
return className+":" + prefix;
}

}

package com.imooc.miaosha.redis;
public class UserKey extends BasePrefix{

private UserKey(String prefix) {
super(prefix);
}
public static UserKey getById = new UserKey("id");
public static UserKey getByName = new UserKey("name");
}



package com.imooc.miaosha.redis;
public class OrderKey extends BasePrefix{

private OrderKey (String prefix) {
super(prefix);
}
public static UserKey getById = new UserKey("id");
public static UserKey getByName = new UserKey("name");
}


举报

相关推荐

关于redis缓存穿透浅析

缓存-Redis

Redis缓存

缓存redis

Redis - 缓存

【Redis】Redis作为缓存

关于Nginx缓存

[Redis] Redis缓存机制

0 条评论