redis緩存的簡單操作(get、put)
本文介紹簡單的redis緩存操作,包括引入jedisjar包、配置redis、RedisDao需要的一些工具、向redis中放數(shù)據(jù)(put)、從redis中取數(shù)據(jù)(get)、訪問redis時的邏輯
一、引入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路徑是我的包路徑,注意你在配置的時候應(yīng)使用你自己的路徑
2)這里使用本地的redis服務(wù)localhost
3)redis服務(wù)的默認端口是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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
redisson中RRateLimiter分布式限流器的使用
Redisson Ratelimiter是Redisson框架中的一種限流算法,用于限制對資源的訪問頻率,本文主要介紹了redisson中RRateLimiter分布式限流器的使用,感興趣的可以了解一下2024-06-06
Redis數(shù)據(jù)庫中實現(xiàn)分布式鎖的方法
這篇文章主要介紹了Redis數(shù)據(jù)庫中實現(xiàn)分布式鎖的方法,Redis是一個高性能的主存式數(shù)據(jù)庫,需要的朋友可以參考下2015-06-06
Spring?Boot?3.0x的Redis?分布式鎖的概念和原理
Redis?分布式鎖是一種基于?Redis?的分布式鎖解決方案,它的原理是利用?Redis?的原子性操作實現(xiàn)鎖的獲取和釋放,從而保證共享資源的獨占性,這篇文章主要介紹了適合?Spring?Boot?3.0x的Redis?分布式鎖,需要的朋友可以參考下2024-08-08
關(guān)于redis可視化工具讀取數(shù)據(jù)亂碼問題
大家來聊一聊在日常操作redis時用的是什么工具,redis提供的一些命令你都了解了嗎,今天通過本文給大家介紹redis可視化工具讀取數(shù)據(jù)亂碼問題,感興趣的朋友跟隨小編一起看看吧2021-07-07

