本文介紹簡單的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的反序列化操作,講序列化對象轉化成我們的需要的對象
2)向redis中放入數(shù)據(jù)時,我們需要先使用protostuff的序列化操作,將對象轉化成序列化對象,才能放入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 RuntimeSchemaObject> 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的構造器中會初始化這個連接池
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; //設置超時時間 一小時,通過超時維護一致性
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- spring結合redis如何實現(xiàn)數(shù)據(jù)的緩存
- Redis整合Spring結合使用緩存實例
- php操作redis緩存方法分享
- 圖文詳解Windows下使用Redis緩存工具的方法
- Python的Flask框架使用Redis做數(shù)據(jù)緩存的配置方法
- PHP使用redis實現(xiàn)統(tǒng)計緩存mysql壓力的方法
- Spring Boot 基于注解的 Redis 緩存使用詳解
- Nginx配置srcache_nginx模塊搭配Redis建立緩存系統(tǒng)
- CI框架中redis緩存相關操作文件示例代碼
- Redis緩存詳解