How spring and redis implement data caching

1. Achieve goals

Cache data through redis. (the purpose is not to speed up the query, but to reduce the burden on the database)

2. Required jar package

Note: there is a corresponding relationship between the versions of jdies and Commons pool jars. Note that the introduction of jar packages should be used in pairs, otherwise an error will be reported. Because the directory structure of Commons pool jar will change according to the version. The previous version was org apache. Pool, and the later version is org apache. pool2...

style="background-color: #0098dd; color: white; font-size: 17px; font-weight: bold;" 3. Introduction to redis

Redis is a key value storage system. Similar to memcached, it supports a relatively large number of stored value types, including string, list, set Zset (sorted set -- ordered set) and hash (hash type). These data types support push / pop, add / remove, intersection, union, difference and richer operations, and these operations are atomic. On this basis, redis supports sorting in different ways. Like memcached, data is cached in memory to ensure efficiency. The difference is that redis will periodically write updated data to the memory Disk or write the modification operation to the additional record file, and on this basis, master-slave (master-slave) is implemented

3. Coding implementation

@H_ 419_ 26@1 ). configuration files (properties)

Configure the parameters that need to be changed frequently as independent properties to facilitate future modification of redis properties

@H_ 419_ 26@2 )、spring-redis. xml

Redis related parameter configuration settings. The value of the parameter comes from the properties file above

@H_ 419_ 26@3 )、applicationContext. xml

Spring's general configuration file, in which the following code

@H_ 419_ 26@4 )、web. xml

Set the total configuration file of spring to load when the project starts

@H_ 419_ 26@5 ). redis caching tool class

Valueoperations -- cache of basic data types and entity classes listoperations -- cache of list setoperations -- cache of set

Cache of hashoperations map

@H_ 419_ 26@6 ), test

The test here is to find out the data of countries and cities in the database when the project starts, cache them, and then remove the data.

6.1 cache data at project startup

6.2 get cached data

Since the beans configured by spring in the configuration file are singleton by default, you only need to inject Autowired to get the original cache class.

The above is how Spring + redis implements data caching. I hope it will be helpful to everyone's learning.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>