Simple redis cache operations (get and put) and redisput

Source: Internet
Author: User

Simple redis cache operations (get and put) and redisput
Simple redis cache operations (get and put)

This article describes simple redis cache operations, including introducing the jedisjar package, some tools required to configure redis and RedisDao, putting data (put) into redis, and getting data (get) from redis) the logic for accessing redis

1. Introduce the jedis jar package
<! -- Java accesses the jar package jedis of redis --> <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> <groupId> com. dyuproject. protostuff </groupId> <artifactId> protostuff-runtime </artifactId> <version> 1.0.8 </version> </dependency>

Note: why should we introduce the serialization dependency jar package protostuff?

1) The data retrieved from redis is serialized. We need to use protostuff deserialization to convert the serialized object to the desired object. 2) when we put data into redis, we need to first use the protostuff serialization operation to convert the object into a serialized object, in order to put it into redis 2. Inject redis into the spring configuration file, put it into the spring ioc container
<! -- Inject 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 during configuration. 2) use the local redis service localhost3) the default port of the redis service is 6379. 3. Some tools required by RedisDao
// Redis connection pool private final JedisPool jedisPool; // generates an empty private RuntimeSchema <Object> schema = RuntimeSchema Based on the Object's bytecode file. createFrom (Object. class); // Object. class: Get the object's bytecode public RedisDao (String ip, int port) {jedisPool = new JedisPool (ip, port );}

Note:

1) RedisDao requires the redis connection pool JedisPool, just like the JDBC database connection pool. We will initialize this connection pool in the RedisDao constructor. 2) We need a tool RuntimeSchema that can generate an empty object based on the object's bytecode file. You can write your Object (Object. class: Get object bytecode file) 3) connection pool JedisPool initialization requires two parameters: ip, port 4. put data (put) to redis)
// Cache the Object to redis public String putObject (Object obj) {// cache logic: object --> serialization --> byte [] --> cache to redis try {Jedis jedis = jedisPool. getResource (); // get the connection Object of redis, which is equivalent to JDBC connection try {String key = "Object:" + obj. getId (); // serialize byte [] bytes = ProtostuffIOUtil. toByteArray (seckill, schema, writable buffer. allocate (Protocol buffer. DEFAULT_BUFFER_SIZE); // if the object is too large, it will be buffered // start to cache int timeout = 60*60; // set the timeout time for one hour, maintain consistency through timeout 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 to redis. That is, serialize the object before writing it to redis! 2) before operating redis, we must obtain the connection object of redis, get data from the connection pool, and get data from redis (get)
// Query public Object getObject (long id) {// redis operation logic try {Jedis jedis = jedisPool. getResource (); // cache the connection Object, which is equivalent to connection try {String key = "Object:" + id; // The Object does not implement internal serialization. // The cache logic is: getByte [] --> deserialization --> Object byte [] bytes = jedis. get (key. getBytes (); // get the serialization object array of the target object from jedis if (bytes! = Null) {// deserialization logic Object obj = schema. newMessage (); // generate a new Null Object ProtostuffIOUtil through schema. mergeFrom (bytes, obj, schema); // return obj;} finally {jedis. close () ;}} catch (Exception e ){
E. printStack ();} return null ;}

Note:

1) Data Retrieval logic: redis --> Get byte [] --> deserialization --> Object2) We put data in the form of a key-Value Pair: id --> obj. When we retrieve data, we obtain the logic based on the id. 6. query the redis logic.

Pseudocode:

Get form redis_cache // first query redisif null // if no get from db // then query from db if null // if no return null still // then return null else // otherwise, put into redis_cache // put the data into redis return obj // then put the data back to else // if return obj is found in redis, the data is returned directly.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.