Simple operations for redis caching (get put)
- 2020-05-30 21:17:27
- OfStack
This article describes simple redis caching operations, including introducing the jedisjar package, configuring redis, RedisDao, putting data into redis (put), fetching data from redis (get), and accessing redis logic
1. Introduce the jedis jar package
<!-- java access redis the jar package jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- protostuff Serialization dependency -->
<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>
Note: why introduce the serialization dependency jar package protostuff?
1) the data extracted from redis is serialized. We need to use the deserialization operation of protostuff to convert the serialized object into the object we need
2) when putting data into redis, we need to use the serialization operation of protostuff to convert the object into a serialized object before putting into redis
2. Inject redis into the spring configuration file and put it into the ioc container of spring
<!-- injection 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>
Note:
1) the RedisDao path here is my package path, note that you should use your own path when configuring
2) local redis service localhost is used here
3) the default port of redis service is 6379
3. RedisDao needs some tools
//redis The connection pool
private final JedisPool jedisPool;// Generates an empty object based on the object's bytecode file
private RuntimeSchema<Object> schema = RuntimeSchema.createFrom(Object.class); //Object.class : gets the bytecode of the object
public RedisDao(String ip, int port){
jedisPool = new JedisPool(ip, port);
}
Note:
1) RedisDao requires redis's connection pool JedisPool, just like JDBC's database connection pool 1. We will initialize the connection pool in RedisDao's constructor
2) we need a tool RuntimeSchema that can generate empty objects according to the object's bytecode file. Whatever object you want to use, you write to your object at Object (Object.class: get the object's bytecode file)
3) the initialization of connection pool JedisPool requires two parameters: ip and port
4. Put data into redis (put)
// Cache the object into redis
public String putObject(Object obj){
// Caching logic :Object --> serialization --> byte[] --> The cache to redis
try {
Jedis jedis = jedisPool.getResource(); // To obtain redis The connection object is equivalent to JDBC the connection
try{
String key = "Object:"+obj.getId();
// serialize
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema,
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); // If the object is too large, it is buffered
// Start the cache
int timeout = 60*60; // Set timeout time 1 hours , Through timeout maintenance 1 consistency
String result = jedis.setex(key.getBytes(), timeout, bytes);
return result;
}finally{
jedis.close();
}
} catch (Exception e) {
e.printStack();
}
return null;
}
Note:
1) cache logic: Object -- > Serialization operation -- > byte[] -- > Write redis. That means serializing the object first, and then writing redis!
2) we must get the connection object of redis from the connection pool before operating redis
5. Data from redis (get)
// from redis Query in cache
public Object getObject(long id){
//redis Operation logic
try {
Jedis jedis = jedisPool.getResource(); // Cache connection objects, equivalent to database connection objects connection
try {
String key = "Object:"+id;
// The entity object does not implement an internal serialization operation
// Cache logic: getByte[] --> deserialization --> Object
byte[] bytes = jedis.get(key.getBytes()); // from jedis Gets an array of serialized objects for the target object
if(bytes != null){
// Deserialization logic
Object obj = schema.newMessage(); // through schema generate 1 Two new empty objects
ProtostuffIOUtil.mergeFrom(bytes, obj, schema); // Do the deserialization operation
return obj;
}
} finally {
jedis.close();
}
} catch (Exception e) {
e.printStack();
}
return null;
}
Note:
1) data fetching logic: redis -- > Get byte - [] > Deserialization -- > Object
2) when we put the data, it is in the form of key-value pairs: id -- > obj. When we fetch data, we fetch it according to id
6. Logic when querying redis
Pseudo code:
get form redis_cache // The first query redis
if null // If there is no
get from db // From the database db The query
if null // If it still doesn't
return null // So return null
else // Otherwise,
put into redis_cache // Now put the data in redis
return obj // Put the data back
else // If from redis Found in
return obj // So just return the data