1. redis in Linux environment has been built or redis using Windows
2. For convenience here Win version , unzip the compressed package downloaded above and click redis server
3. Start
Note: be sure to start when using!!
4. Introduction to springdataredis
4.1 thoughts on common problems of the project
Our current system has realized advertising background management and advertising front display, but a large number of people visit the home page every day, causing great access pressure on the database, or even paralysis. So how to solve it? We usually do it in two ways: one is data caching and the other is web page static. Let's discuss the first solution today.
4.2 Redis
Redis is an open source key value database, which runs in memory and is written by ANSI C. Redis is usually used for enterprise development to implement caching. Similar products include memcache, memcached, MongoDB, etc.
4.3 Jedis
Jedis is a Java oriented client officially launched by Redis, which provides many interfaces for Java language calls. It can be downloaded from the official website of Redis. Of course, there are some clients provided by open source enthusiasts, such as Jredis, SRP, etc. jedis is recommended.
4.4 Spring Data Redis
Spring data redis is a part of the spring family. It provides access to redis services through simple configuration in srping applications, and highly encapsulates the underlying development packages of reids (Jedis, JRedis, and RJC). RedisTemplate provides various redis operations, exception handling and serialization, supports publishing and subscription, and implements spring 3.1 cache.
Spring data redis provides the following functions for jedis:
1. Automatic connection pool management provides a highly encapsulated "RedisTemplate" class
2. Classify and encapsulate a large number of APIs in jedis client, and encapsulate the same type of operation as operation interface
ValueOperations: simple K-V operation
SetOperations: set type data operations
ZSetOperations: zset type data operation
HashOperations: data operations for map type
ListOperations: data operations for list type
4.5 Spring Data Redis introduction Demo
4.5.1 preparation
(1) Build Maven project SpringDataRedisDemo
(2) Introduce Spring related dependencies and JUnit dependencies
(3) Introduce Jedis and SpringDataRedis dependencies
pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>lhz.lx</groupId> <artifactId>SpringDataRedisDemo</artifactId> <version>1.0-SNAPSHOT</version> <!-- Centralized definition dependent version number --> <properties> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <!-- cache --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> </dependencies> </project>
(4) Create the properties folder under src/main/resources and create redis config properties
redis.host=127.0.0.1 redis.port=6379 redis.pass= redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true
(5) Create the spring folder under src/main/resources and create ApplicationContext redis xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:property-placeholder location="classpath*:properties/*.properties" /> <!-- redis Related configuration --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean> <beans> <!-- maxIdle : Maximum idle maxWaitMillis:Maximum number of milliseconds to wait while connecting testOnBorrow: Extracting a jedis Whether to verify the instance in advance; If yes true,Then you get jedis All instances are available;-->
4.5.2 value type operation
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestValue { @Autowired private RedisTemplate redisTemplate; @Test public void setValue(){ redisTemplate.boundValueOps("name").set("root"); } @Test public void getValue(){ String str = (String) redisTemplate.boundValueOps("name").get(); System.out.println(str); } @Test public void deleteValue(){ redisTemplate.delete("name");; } }
4.5.3 Set type operation
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestSet { @Autowired private RedisTemplate redisTemplate; /** * Stored value */ @Test public void setValue(){ redisTemplate.boundSetOps("nameset").add("Cao Cao"); redisTemplate.boundSetOps("nameset").add("Liu Bei"); redisTemplate.boundSetOps("nameset").add("Sun Quan"); } /** * Extract value */ @Test public void getValue(){ Set members = redisTemplate.boundSetOps("nameset").members(); System.out.println(members); } /** * Deletes a value from the collection */ @Test public void deleteValue(){ redisTemplate.boundSetOps("nameset").remove("Sun Quan"); } /** * Delete entire collection */ @Test public void deleteAllValue(){ redisTemplate.delete("nameset"); } }
4.5.4 List type operation
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestList { @Autowired private RedisTemplate redisTemplate; /** * Right stack: objects added after are placed behind *range(0, 10); From 0 to the first few, not all methods of query are provided */ @Test public void testSetValue1(){ redisTemplate.boundListOps("namelist1").rightPush("Liu Bei"); redisTemplate.boundListOps("namelist1").rightPush("Guan Yu"); redisTemplate.boundListOps("namelist1").rightPush("Fei Zhang"); } /** * Display right stack collection */ @Test public void testGetValue1(){ List list = redisTemplate.boundListOps("namelist1").range(0, 10); System.out.println(list); } //result //[Liu Bei, Guan Yu, Zhang Fei] /** * Left stack: objects added after are arranged in front */ @Test public void testSetValue2(){ redisTemplate.boundListOps("namelist2").leftPush("Liu Bei"); redisTemplate.boundListOps("namelist2").leftPush("Guan Yu"); redisTemplate.boundListOps("namelist2").leftPush("Fei Zhang"); } /** * Display left stack collection *range(0, 10); From 0 to the first few, not all methods of query are provided */ @Test public void testGetValue2(){ List list = redisTemplate.boundListOps("namelist2").range(0, 10); System.out.println(list); } //result //[Zhang Fei, Guan Yu, Liu Bei] @Test public void testSearchByIndex(){ String s = (String) redisTemplate.boundListOps("namelist2").index(2); System.out.println(s); } //result //Guan Yu /** * Remove an element from the collection */ @Test public void testRemoveByIndex(){ //The first parameter represents the number of removed redisTemplate.boundListOps("namelist2").remove(1, "Guan Yu"); } //result //[Zhang Fei, Liu Bei] }
4.5.5 Hash type operation
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestHash { @Autowired private RedisTemplate redisTemplate; //(1) Stored value @Test public void testSetValue(){ redisTemplate.boundHashOps("namehash").put("a", "Tang Monk"); redisTemplate.boundHashOps("namehash").put("b", "name of a fictitious monkey with supernatural powers"); redisTemplate.boundHashOps("namehash").put("c", "Bajie"); redisTemplate.boundHashOps("namehash").put("d", "Monk Sha"); } //(2) Extract all keys @Test public void testGetKeys(){ Set s = redisTemplate.boundHashOps("namehash").keys(); System.out.println(s); } //Operation results: //[a, b, c, d] //(3) Extract all values @Test public void testGetValues(){ List values = redisTemplate.boundHashOps("namehash").values(); System.out.println(values); } //Operation results: //[Tang monk, Wukong, Bajie, Sha monk] //(4) Extract value according to KEY @Test public void testGetValueByKey(){ Object object = redisTemplate.boundHashOps("namehash").get("b"); System.out.println(object); } //Operation results: //name of a fictitious monkey with supernatural powers //(5) Remove value according to KEY @Test public void testRemoveValueByKey(){ redisTemplate.boundHashOps("namehash").delete("c"); } //View the collection content again after running: //[Tang monk, Wukong, Sha monk] }
Demo download: https://download.csdn.net/download/zhuocailing3390/10785961