redis緩存的簡(jiǎn)單操作(get、put)
本文介紹簡(jiǎn)單的redis緩存操作,包括引入jedisjar包、配置redis、RedisDao需要的一些工具、向redis中放數(shù)據(jù)(put)、從redis中取數(shù)據(jù)(get)、訪問(wèn)redis時(shí)的邏輯
一、引入jedis jar包
<!-- java訪問(wèn)redis的jar包jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> <!-- protostuff序列化依賴(lài) --> <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>
注意:為什么要引入序列化依賴(lài)jar包protostuff?
1)從redis中取出的數(shù)據(jù)是序列化的,我們需要使用protostuff的反序列化操作,講序列化對(duì)象轉(zhuǎn)化成我們的需要的對(duì)象
2)向redis中放入數(shù)據(jù)時(shí),我們需要先使用protostuff的序列化操作,將對(duì)象轉(zhuǎn)化成序列化對(duì)象,才能放入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路徑是我的包路徑,注意你在配置的時(shí)候應(yīng)使用你自己的路徑
2)這里使用本地的redis服務(wù)localhost
3)redis服務(wù)的默認(rèn)端口是6379
三、RedisDao需要的一些工具
//redis連接池 private final JedisPool jedisPool;//根據(jù)對(duì)象的字節(jié)碼文件,生成空對(duì)象 private RuntimeSchema<Object> schema = RuntimeSchema.createFrom(Object.class); //Object.class:獲取對(duì)象的字節(jié)碼 public RedisDao(String ip, int port){ jedisPool = new JedisPool(ip, port); }
注意:
1)RedisDao需要redis的連接池JedisPool,就好比JDBC的數(shù)據(jù)庫(kù)連接池一樣。我們?cè)赗edisDao的構(gòu)造器中會(huì)初始化這個(gè)連接池
2)我們需要一個(gè)可以根據(jù)對(duì)象的字節(jié)碼文件生成空對(duì)象的工具 RuntimeSchema。你要使用什么對(duì)象,你就在Object的位置寫(xiě)入你的對(duì)象(Object.class:獲取對(duì)象的字節(jié)碼文件)
3)連接池JedisPool的初始化需要兩個(gè)參數(shù):ip、port
四、向redis中放數(shù)據(jù)(put)
//將對(duì)象緩存到redis public String putObject(Object obj){ //緩存邏輯:Object --> 序列化 --> byte[] --> 緩存到redis try { Jedis jedis = jedisPool.getResource(); //獲取redis的連接對(duì)象,相當(dāng)于JDBC的connection try{ String key = "Object:"+obj.getId(); //進(jìn)行序列化 byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); //如果對(duì)象過(guò)大,會(huì)進(jìn)行緩沖 //開(kāi)始緩存 int timeout = 60*60; //設(shè)置超時(shí)時(shí)間 一小時(shí),通過(guò)超時(shí)維護(hù)一致性 String result = jedis.setex(key.getBytes(), timeout, bytes); return result; }finally{ jedis.close(); } } catch (Exception e) { e.printStack(); } return null; }
注意:
1)緩存邏輯:Object --> 序列化操作 --> byte[] --> 寫(xiě)入redis。也就是先把對(duì)象序列化,再寫(xiě)入redis!
2)我們?cè)诓僮鱮edis之前必須先拿到redis的連接對(duì)象,從連接池拿
五、從redis中取數(shù)據(jù)(get)
//從redis緩存中查詢(xún) public Object getObject(long id){ //redis操作邏輯 try { Jedis jedis = jedisPool.getResource(); //緩存連接對(duì)象,相當(dāng)于數(shù)據(jù)庫(kù)連接對(duì)象connection try { String key = "Object:"+id; //實(shí)體對(duì)象并沒(méi)有實(shí)現(xiàn)內(nèi)部序列化操作 //緩存邏輯:getByte[] --> 反序列化 --> Object byte[] bytes = jedis.get(key.getBytes()); //從jedis中獲取目標(biāo)對(duì)象的序列化對(duì)象數(shù)組 if(bytes != null){ //反序列化邏輯 Object obj = schema.newMessage(); //通過(guò)schema生成一個(gè)新的空對(duì)象 ProtostuffIOUtil.mergeFrom(bytes, obj, schema); //進(jìn)行反序列化操作 return obj; } } finally { jedis.close(); } } catch (Exception e) { e.printStack(); } return null; }
注意:
1)取數(shù)據(jù)邏輯:redis --> 得到byte[] --> 反序列化 --> Object
2)我們?cè)诜艛?shù)據(jù)的時(shí)候,是以鍵值對(duì)的形式:id --> obj。我們?cè)谌?shù)據(jù)的時(shí)候,就是根據(jù)id來(lái)取的
六、查詢(xún)r(jià)edis時(shí)的邏輯
偽代碼:
get form redis_cache //首先查詢(xún)r(jià)edis if null //如果沒(méi)有 get from db //再?gòu)臄?shù)據(jù)庫(kù)db查詢(xún) if null //如果仍然沒(méi)有 return null //那么返回空 else //否則 put into redis_cache //現(xiàn)將數(shù)據(jù)放入redis return obj //再放回?cái)?shù)據(jù) else //如果從redis中查詢(xún)到了 return obj //那么直接返回?cái)?shù)據(jù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializ
本文主要介紹了Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Redis常見(jiàn)限流算法原理及實(shí)現(xiàn)
這篇文章主要介紹了Redis常見(jiàn)限流算法原理及實(shí)現(xiàn),限流簡(jiǎn)稱(chēng)流量限速(Rate?Limit)是指只允許指定的事件進(jìn)入系統(tǒng),超過(guò)的部分將被拒絕服務(wù)、排隊(duì)或等待、降級(jí)等處理2022-08-08Redis內(nèi)部數(shù)據(jù)結(jié)構(gòu)Dict的實(shí)現(xiàn)方法
這篇文章主要介紹了Redis內(nèi)部數(shù)據(jù)結(jié)構(gòu)Dict的實(shí)現(xiàn)方法,本篇文章所述的dict在Redis中最主要的作用就是用于維護(hù)Redis數(shù)據(jù)庫(kù)中所有Key、value映射的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2022-05-05Redis實(shí)戰(zhàn)之Jedis使用技巧詳解
Jedis?是老牌的?Redis?的?Java?客戶(hù)端,提供了比較全面的?Redis?命令的操作支持,也是目前使用最廣泛的客戶(hù)端。這篇文章主要為大家詳細(xì)介紹了Jedis的使用技巧,需要的可以參考一下2022-12-12使用redis實(shí)現(xiàn)延遲通知功能(Redis過(guò)期鍵通知)
這篇文章主要介紹了使用redis實(shí)現(xiàn)延遲通知功能(Redis過(guò)期鍵通知)的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09