欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

redis緩存的簡單操作(get、put)

本文介紹簡單的redis緩存操作,包括引入jedisjar包、配置redis、RedisDao需要的一些工具、向redis中放數(shù)據(jù)(put)、從redis中取數(shù)據(jù)(get)、訪問redis時的邏輯

創(chuàng)新互聯(lián)公司專注于成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務”的宗旨,從客戶的利益和觀點出發(fā),讓客戶在網(wǎng)絡營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴謹?shù)膽B(tài)度對待客戶,用專業(yè)的服務創(chuàng)造價值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

一、引入jedis jar包

<!-- java訪問redis的jar包jedis -->
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.7.3</version>
</dependency>
<!-- protostuff序列化依賴 -->
<dependency>
 <groupId>com.dyuproject.protostuff</groupId>
 <artifactId>protostuff-core</artifactId>
 <version>1.0.8</version>
</dependency>
<dependency>
 <groupId>com.dyuproject.protostuff</groupId>
 <artifactId>protostuff-runtime</artifactId>
 <version>1.0.8</version>
</dependency>

注意:為什么要引入序列化依賴jar包protostuff?

1)從redis中取出的數(shù)據(jù)是序列化的,我們需要使用protostuff的反序列化操作,講序列化對象轉(zhuǎn)化成我們的需要的對象

2)向redis中放入數(shù)據(jù)時,我們需要先使用protostuff的序列化操作,將對象轉(zhuǎn)化成序列化對象,才能放入redis

二、在spring配置文件中注入redis,放入spring的ioc容器

<!-- 注入redis dao -->
<bean id="redisDao" class="org.demo.dao.cache.RedisDao">
  <constructor-arg index="0" value="localhost"></constructor-arg>
  <constructor-arg index="1" value="6379"></constructor-arg>
</bean>

注意:

1)這里的RedisDao路徑是我的包路徑,注意你在配置的時候應使用你自己的路徑

2)這里使用本地的redis服務localhost

3)redis服務的默認端口是6379

三、RedisDao需要的一些工具

//redis連接池
 private final JedisPool jedisPool;//根據(jù)對象的字節(jié)碼文件,生成空對象
 private RuntimeSchema<Object> schema = RuntimeSchema.createFrom(Object.class); //Object.class:獲取對象的字節(jié)碼
 
 public RedisDao(String ip, int port){
  jedisPool = new JedisPool(ip, port);
 }

注意:

1)RedisDao需要redis的連接池JedisPool,就好比JDBC的數(shù)據(jù)庫連接池一樣。我們在RedisDao的構(gòu)造器中會初始化這個連接池

2)我們需要一個可以根據(jù)對象的字節(jié)碼文件生成空對象的工具 RuntimeSchema。你要使用什么對象,你就在Object的位置寫入你的對象(Object.class:獲取對象的字節(jié)碼文件)

3)連接池JedisPool的初始化需要兩個參數(shù):ip、port

四、向redis中放數(shù)據(jù)(put)

//將對象緩存到redis
 public String putObject(Object obj){
  //緩存邏輯:Object --> 序列化 --> byte[] --> 緩存到redis
  try {
   Jedis jedis = jedisPool.getResource(); //獲取redis的連接對象,相當于JDBC的connection
   try{
    String key = "Object:"+obj.getId();
    //進行序列化
    byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema, 
      LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); //如果對象過大,會進行緩沖
    //開始緩存
    int timeout = 60*60; //設(shè)置超時時間 一小時,通過超時維護一致性
    String result = jedis.setex(key.getBytes(), timeout, bytes);
    return result;
   }finally{
    jedis.close();
   }
  } catch (Exception e) {
   e.printStack();
  }
  return null;
 }

注意:

1)緩存邏輯:Object --> 序列化操作 --> byte[] --> 寫入redis。也就是先把對象序列化,再寫入redis!

2)我們在操作redis之前必須先拿到redis的連接對象,從連接池拿

五、從redis中取數(shù)據(jù)(get)

 //從redis緩存中查詢
 public Object getObject(long id){
  //redis操作邏輯
  try {
   Jedis jedis = jedisPool.getResource(); //緩存連接對象,相當于數(shù)據(jù)庫連接對象connection
   try {
    String key = "Object:"+id;
    //實體對象并沒有實現(xiàn)內(nèi)部序列化操作
    //緩存邏輯:getByte[] --> 反序列化 --> Object
    byte[] bytes = jedis.get(key.getBytes()); //從jedis中獲取目標對象的序列化對象數(shù)組
    if(bytes != null){
     //反序列化邏輯
     Object obj = schema.newMessage(); //通過schema生成一個新的空對象
     ProtostuffIOUtil.mergeFrom(bytes, obj, schema); //進行反序列化操作
     return obj;
    }
    
   } finally {
    jedis.close();
   }
    
  } catch (Exception e) {
        e.printStack();
  }
  return null;
 }

注意:

1)取數(shù)據(jù)邏輯:redis --> 得到byte[] --> 反序列化 --> Object

2)我們在放數(shù)據(jù)的時候,是以鍵值對的形式:id --> obj。我們在取數(shù)據(jù)的時候,就是根據(jù)id來取的

六、查詢redis時的邏輯

偽代碼:

get form redis_cache    //首先查詢redis
if null       //如果沒有
 get from db     //再從數(shù)據(jù)庫db查詢
 if null      //如果仍然沒有
  return null    //那么返回空
 else       //否則
  put into redis_cache  //現(xiàn)將數(shù)據(jù)放入redis
  return obj    //再放回數(shù)據(jù)
else        //如果從redis中查詢到了
 return obj     //那么直接返回數(shù)據(jù)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當前名稱:redis緩存的簡單操作(get、put)
網(wǎng)址分享:http://chinadenli.net/article8/pgjiop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司品牌網(wǎng)站制作品牌網(wǎng)站設(shè)計定制網(wǎng)站外貿(mào)網(wǎng)站建設(shè)網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管