Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple operation of redis cache (get, put)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces simple redis caching operations, including introducing jedisjar packages, configuring redis, some tools required by RedisDao, putting data into redis (put), fetching data from redis (get), and logic when accessing redis

First, introduce jedis jar package

Redis.clients jedis 2.7.3 com.dyuproject.protostuff protostuff-core 1.0.8 com.dyuproject.protostuff protostuff-runtime 1.0.8

Note: why introduce 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 objects into serialized objects before putting them into redis

Second, inject redis into the spring configuration file and put it into the ioc container of spring

Note:

1) the RedisDao path here is my package path. Please note that you should use your own path when configuring it.

2) the local redis service localhost is used here

3) the default port for redis service is 6379

III. Some tools needed by RedisDao

/ / redis connection pool private final JedisPool jedisPool;// generates an empty object private RuntimeSchema schema = RuntimeSchema.createFrom (Object.class) based on the object's bytecode file; / / Object.class: get the object's bytecode public RedisDao (String ip, int port) {jedisPool = new JedisPool (ip, port);}

Note:

1) RedisDao needs redis's connection pool JedisPool, just like JDBC's database connection pool. We initialize this connection pool in the constructor of RedisDao

2) We need a tool RuntimeSchema that can generate empty objects based on the bytecode file of the object. Which object you want to use, you write your object in the location of Object (Object.class: get the bytecode file of the object)

3) initialization of connection pool JedisPool requires two parameters: ip and port

4. Put data into redis (put)

/ / caching objects to redis public String putObject (Object obj) {/ / cache logic: Object-- > serialization-- > byte []-- > caching to redis try {Jedis jedis = jedisPool.getResource (); / / get the connection object of redis, which is equivalent to JDBC's 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, buffer / / start caching int timeout = 60Secret60; / / set the timeout for one hour to 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 redis. That is, serialize the object first and then write it to redis!

2) before operating redis, we must first get the connection object of redis, and get it from the connection pool

5. Fetch data from redis (get)

/ / query public Object getObject (long id) {/ / redis operation logic try {Jedis jedis = jedisPool.getResource () from redis cache; / / cache connection object, which is equivalent to database connection object connection try {String key = "Object:" + id / / the entity object does not implement the internal serialization operation / / cache logic: getByte []-> deserialization-> Object byte [] bytes = jedis.get (key.getBytes ()); / / get the serialized object array if (bytes! = null) {/ / deserialization logic Object obj = schema.newMessage () from jedis / generate a new empty object ProtostuffIOUtil.mergeFrom (bytes, obj, schema) via schema; / / perform deserialization operation return obj;}} finally {jedis.close ();}} catch (Exception e) {e.printStack ();} return null;}

Note:

1) fetch data logic: redis-- > get byte []-- > deserialization-- > Object

2) when we put data, it is in the form of key-value pairs: id-- > obj. When we get the data, we get it according to id.

VI. Logic when querying redis

Pseudo code:

Get form redis_cache / / first query redisif null / / if there is no get from db / / then query if null from the database db / / if there is still no return null / / then return empty else / / otherwise put into redis_cache / / now put the data into redis return obj / / and then return the data else / / if you get return obj / / from redis, then return the data directly

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report