一文解析redis如何存储对象和集合

 行业动态     |      2019-10-14 14:18

在项目中,缓存以及mq消息队列可以说是不可或缺的2个重要技术。前者主要是为了减轻数据库压力,大幅度提升性能。后者主要是为了提高用户的体验度,我理解的是再后端做的一个ajax请求,并且像ribbmitmq等消息队列有重试机制等功能。

这里主要讲redis如何把对象,集合存入,并且取出.

private Jedis jedis;private JedisPoolConfig config;private JedisShardInfo sharInfo;@Beanpublic Jedis jedis{//连接redis服务器,192.168.0.100:6379// jedis = new Jedis;// //权限认证// jedis.auth;// 操作单独的文本串config = new JedisPoolConfig;
config.setMaxIdle;//最大空闲时间config.setMaxWaitMillis; //最大等待时间config.setMaxTotal; //redis池中最大对象个数sharInfo = new JedisShardInfo;
sharInfo.setPassword;
sharInfo.setConnectionTimeout;//链接超时时间jedis = new Jedis;return jedis;
复制代码
spring:
 redis:
 database: 0
 host: 101.132.191.77
 port: 6379
 password: 123456
 pool:
 max-idle: 8 #连接池最大连接数
 min-idle: 0 # 连接池中的最小空闲连接
 max-active: 8 # 连接池最大阻塞等待时间
 max-wait: -1 # 连接池中的最大空闲连接
 timeout: 5000 # 连接超时时间
复制代码
凯发k8真人import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/**
public class SerializeUtil 
 public static byte[] serialize {
 ObjectOutputStream oos = null;
 ByteArrayOutputStream baos = null;
 try {// 序列化baos = new ByteArrayOutputStream;
 oos = new ObjectOutputStream;
 oos.writeObject;
 byte[] bytes = baos.toByteArray;
 return bytes;
 } catch  {
 }return null;
 public static Object unserialize {
 ByteArrayInputStream bais = null;
 try {
 // 反序列化bais = new ByteArrayInputStream;
 ObjectInputStream ois = new ObjectInputStream;
 return ois.readObject;
 } catch  {
 }return null;
复制代码

redisServiceImpl.set主要是传object,setStr主要设置string

import com.ys.util.redis.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl
 {@Autowired 
 private StringRedisTemplate stringRedisTemplate;
 @Autowired
 private Jedis jedis;
 public void setStr {
 setStr;
 public void setStr
 {if{
 return;
 }if{
 String obj =  value;
 stringRedisTemplate.opsForValue.set;
 }else if{
 List obj =  value;
 stringRedisTemplate.opsForList.leftPushAll;
 }else if{
 Map obj =  value;
 stringRedisTemplate.opsForHash.putAll;
 }if 
 stringRedisTemplate.expire;
 public Object getKey 
 {return stringRedisTemplate.opsForValue.get;
 public void delKey {
 stringRedisTemplate.delete;
 public boolean del
 {return jedis.del) 
复制代码
import com.ys.service.impl.RedisServiceImpl;
import com.ys.vo.IqProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
public class RedisServiceController
@Autowired
private RedisServiceImpl redisService;
@RequestMapping
public String setredis{
 redisService.setStr;
 return "保存成功,请访问getredis查询redis";
@RequestMapping
public String setObj{
 IqProduct iqProduct = new IqProduct;
 iqProduct.setSort;
 iqProduct.setTimestamp.getTime);
 iqProduct.setProductName;
 // list.add;
 redisService.set;
 return "保存成功,请访问getredis查询redis";
 @RequestMapping
 public Object getObj{
 Object object = redisService.get;
 if{
 IqProduct iqProduct =  object;
 System. out.println);
 System. out.println);
 System. out.println);
 }return object;
 @RequestMapping
 public boolean delObj
 {boolean del = redisService.del;
 return del;
 @RequestMapping
 public String getredis{
 String getredis =  redisService.getKey;
 return "redis的key是=== "+getredis;
 @RequestMapping
 public String delredis{
 redisService.delKey;
 return "删除成功,请通过getredis进行查询";
 @RequestMapping
 public String setList{
 List list = new ArrayList;for {
 IqProduct iqProduct = new IqProduct;
 iqProduct.setSort;
 iqProduct.setTimestamp.getTime);
 iqProduct.setProductName;
 list.add;
 redisService.set;
 return "保存成功,请访问getredis查询redis";
 @RequestMapping
 public Object getList{
 Object object = redisService.get;
 if{
 List IqProduct iqProducts =  object;
 for ;i++){
 IqProduct iqProduct = iqProducts.get;
 System. out.println);
 System. out.println);
 System. out.println);
 }return object;
 @RequestMapping
 public boolean delList
 boolean del = redisService.del;return del;
复制代码