复述,如何存储对象

  介绍

这篇文章主要介绍复述,如何存储对象,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

复述是一个键值存储系统。和Memcached类似,它支持存储值的类型相对更多,包括字符串(字符串),列表(链表),设置(集合),zset(排序设置——有序集合)和散列(哈希类型)本文介绍了关于复述是如何存储对象与集合的相关资料,需要的朋友可以参考下

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

这里主要讲复述,如何把对象,集合存入,并且取出。下面话不多说了,来一起看看详细的介绍吧。


春:
  复述:
  数据库:0
  主持人:101.132.191.77
  端口:6379
  密码:123456
  池:
  最大空闲:8 #连接池最大连接数(使用负值表示没有限制)
  min-idle: 0 #连接池中的最小空闲连接
  max-active: 8 #连接池最大阻塞等待时间(使用负值表示没有限制)
  最大等待:1 #连接池中的最大空闲连接
  超时:5000 #连接超时时间(毫秒)


进口java.io.ByteArrayInputStream;
  进口java.io.ByteArrayOutputStream;
  进口java.io.ObjectInputStream;
  进口java.io.ObjectOutputStream;/* *
  公开课SerializeUtil
  {
  公共静态byte[]序列化对象(对象){
  ObjectOutputStream oos=零;
  ByteArrayOutputStream包=零;
  尝试{//序列化包=new ByteArrayOutputStream ();
  oos=new ObjectOutputStream(包);
  oos.writeObject(对象);
  byte[]字节=baos.toByteArray ();
  返回字节;
  }捕捉(异常e) {
  
  }返回null;
  }
  公共静态对象unserialize (byte[]字节){
  ByteArrayInputStream姨=零;
  尝试{//反序列化姨=new ByteArrayInputStream(字节);
  ObjectInputStream ois=new ObjectInputStream(姨);
  返回ois.readObject ();
  }捕捉(异常e) {
  
  }返回null;
  }
  }


进口com.ys.util.redis.SerializeUtil;
  进口org.springframework.beans.factory.annotation.Autowired;
  进口org.springframework.data.redis.core.StringRedisTemplate;
  进口org.springframework.stereotype.Service;
  进口redis.clients.jedis.Jedis;
  进口并不知道;
  进口java.util.Map;
  进口java.util.concurrent.TimeUnit;
  @ service
  公开课RedisServiceImpl
  
  {@ autowired
  
  私人StringRedisTemplate StringRedisTemplate;
  @ autowired
  私人能能;
  公共空间setStr(字符串,字符串值){
  setStr(价值,关键零);
  }
  公共空间setStr(字符串键,对象的价值,长时间)
  {如果(value=https://www.yisu.com/zixun/=null) {
  返回;
  }如果(值运算符字符串){
  字符串obj=(字符串)值;
  stringRedisTemplate.opsForValue()。集(关键、obj);
  }else if(值运算符列表){
  obj=列表(列表)值;
  stringRedisTemplate.opsForList () .leftPushAll(关键、obj);
  }else if(价值instanceof地图){
  地图obj=(地图)值;
  stringRedisTemplate.opsForHash () .putAll(关键、obj);
  }如果(时间!=null)
  stringRedisTemplate。(到期时间,关键TimeUnit.SECONDS);
  }
  公共对象getKey (String键)
  {返回stringRedisTemplate.opsForValue () . get(关键);
  }
  公共空间delKey (String键){
  stringRedisTemplate.delete(关键);
  }
  公共布尔德尔(String键)
  {返回jedis.del (key.getBytes ())> 0;
  }
  }


进口com.ys.service.impl.RedisServiceImpl;
  进口com.ys.vo.IqProduct;
  进口org.springframework.beans.factory.annotation.Autowired;
  进口org.springframework.web.bind.annotation.RequestMapping;
  进口org.springframework.web.bind.annotation.RestController;
  进口java.util.ArrayList;
  进口java.util.Date;
  进口并不知道;
  @RestController
  
  公开课RedisServiceController
  
  {
  @ autowired
  
  私人RedisServiceImpl redisService;
  @RequestMapping (value=https://www.yisu.com/zixun/"/setredis”)
  setredis公共字符串(字符串keyredis) {
  redisService.setStr (keyredis, 2018年1月26日);
  返回“保存成功,请访问getredis查询复述”;
  }
  @RequestMapping (value="/setObj ")
  setObj公共字符串(字符串keyredis) {
  IqProduct IqProduct=new IqProduct ();
  iqProduct.setSort (1);
  iqProduct。.getTime setTimestamp(新日期()());
  iqProduct.setProductName (“productname”);//list.add (iqProduct);
  redisService。集(keyredis iqProduct);
  返回“保存成功,请访问getredis查询复述”;
  }
  @RequestMapping (value="/getObj ")
  公共对象getObj(字符串keyredis) {
  对象对象=redisService.get (keyredis);
  如果(对象!=null) {
  IqProduct IqProduct=(IqProduct)对象;
  系统。out.println (iqProduct.getProductName ());
  系统。out.println (iqProduct.getId ());
  系统。out.println (iqProduct.getTimestamp ());
  }返回对象;
  }
  @RequestMapping (value="/delObj ")
  公共布尔delObj(字符串keyredis)
  {布尔德尔=redisService.del (keyredis);
  返回del;
  }
  @RequestMapping (value="/getredis ")
  getredis公共字符串(字符串keyredis) {
  字符串getredis=(字符串)redisService.getKey (keyredis);
  返回“复述的关键是===> " + getredis;
  }
  @RequestMapping (value="/delredis ")
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

复述,如何存储对象