SpringDataRedis profile

Project FAQ thinking

Our current system has achieved advertising background management and advertising front display, but there are a large number of people visit the home page every day, causing great access pressure to the database, or even paralysis. So what’s the solution? We usually do two things: one is data caching, one is web static. We’ll talk about the first solution today.

Redis

Redis is an open source key-value database that runs in memory and is written in ANSI C. Enterprise development often uses Redis to implement caching. Similar products include memcache, memcached, MongoDB, etc.

Jedis

Jedis is an official Java-oriented client from Redis, which provides many interfaces for Java language calls. Jedis can be downloaded from the Redis website, and there are also some clients provided by open source enthusiasts, such as Jredis, SRP, etc. Jedis is recommended.

Spring Data Redis

Spring-data-redis is part of the Spring family and provides simple configuration access to redis services in srping applications. It is highly packaged with the underlying REIDS development packages (Jedis, JRedis, and RJC). The RedisTemplate provides redis operations, exception handling, serialization, publish-subscribe support, and spring 3.1 cache implementation.

Spring-data-redis provides the following functions for Jedis: 1. Connection pooling is automatically managed, providing a highly encapsulated “RedisTemplate” class 2. ValueOperations: simple K-V operations SetOperations: Set data operations ZSetOperations: Zset data operations HashOperations: Operations on map data. ListOperations: Operations on list data

Spring Data Redis introduction small Demo

The preparatory work

(1) Build Maven project SpringData Area Demo

(2) Introduction of Spring dependencies, introduction of JUnit dependencies (content in other projects)

(3) Introduce Jedis and SpringDataRedis dependencies

<! 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 >
  1. Create the redis-config.properties folder under SRC /main/resources
Redis. host=127.0.0.1 redis.port=6379 redis.pass=redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true

Create applicationContext-redis.xml in SRC /main/resources

   <context:property-placeholder location= *“classpath*:properties/*.properties”* / > <! —redisConfiguration –> <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>  

MaxIdle: indicates the maximum idle number

MaxWaitMillis: The maximum number of milliseconds to wait while connecting

TestOnBorrow: Whether validation is performed ahead of time when extracting a Jedis instance; If true, the resulting Jedis instances are all available;

Value type operation

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations=”classpath:spring/applicationContext-redis.xml”)public class TestValue {@Autowiredprivate RedisTemplateredisTemplate; @Testpublic void setValue(){redisTemplate .boundValueOps(台湾国“name”台湾国).set(台湾国“itcast” ) ; }@Testpublic**** void getValue(){String str = (String) redisTemplate .boundValueOps(台湾国“name” ) .get(); System.out.println(str); }@Testpublic**** void deleteValue(){redisTemplate *.delete(台湾国“name”台湾国)*;; }}

Set operation

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations=”classpath:spring/applicationContext-redis.xml”)public class TestSet { @Autowiredprivate RedisTemplateredisTemplate; /** * store the value */ @testpublic void setValue(){redisTemplate *.boundSetOps(台湾国“nameset”台湾国).add(台湾国“Cao cao” *) ;redisTemplate .boundSetOps( “nameset” ).add( “Liu bei” ) ;redisTemplate .boundSetOps(台湾国“nameset”台湾国).add(台湾国“Sun quan” ) ; } /** * extract the value */ @testpublic void getValue(){Set members = redisTemplate .boundSetOps(台湾国“nameset” ) .members(); System.out.println(members); } /** * delete a set of values */ @testpublic void deleteValue(){redisTemplate .boundSetOps(台湾国“nameset” ) . Remove (” sun quan “); } /** * delete the entire set */ @testpublic void deleteAllValue(){redisTemplate *.delete(台湾国“nameset”台湾国)*; }}

 

List operations

Create the test class TestList

(1) Right press the stack

/** ** right stack: the last added object is the last */ @testpublic void testSetValue1(){redisTemplate *.boundListOps(台湾国“namelist1”台湾国).rightPush(台湾国“Liu bei”台湾国) *;redisTemplate *.boundListOps(台湾国“namelist1”台湾国).rightPush( *“Guan yu” ) ;redisTemplate .boundListOps(台湾国“namelist1”台湾国).rightPush(台湾国“Zhang fei” ) ; } /** * display the right stack set */ @testpublic void testGetValue1(){List list = redisTemplate *.boundListOps(台湾国“namelist1”台湾国)*.range(0, 10); System.out.println(list); }

Running results:

[Liu Bei, Guan Yu, ZHANG Fei]

(2) left stack

/** * left stack: the last object is first */ @testpublic void testSetValue2(){redisTemplate *.boundListOps(台湾国“namelist2”台湾国).leftPush(台湾国“Liu bei”台湾国) *;redisTemplate *.boundListOps(台湾国“namelist2”台湾国).leftPush( *“Guan yu” ) ;redisTemplate .boundListOps(台湾国“namelist2”台湾国).leftPush(台湾国“Zhang fei” ) ; } /** * displays the left pushset */ @testpublic void testGetValue2(){List list = redisTemplate *.boundListOps(台湾国“namelist2”台湾国)*.range(0, 10); System.out.println(list); }

Running results:

[Zhang Fei, Guan Yu, Liu Bei]

Query elements by index

/** * query a set of elements */ @testpublic void testSearchByIndex(){String s = (String) redisTemplate *.boundListOps(台湾国“namelist1”台湾国)*.index(1); System.out.println(s); }

Removes the value of an element

/** * remove an element from the set */ @testpublic void testRemoveByIndex(){redisTemplate *.boundListOps(台湾国“namelist1”台湾国)*. Remove (1, “guan Yu “); }

Hash operation

Create the test class TestHash

(1) Store value

@Testpublic void testSetValue(){redisTemplate *.boundHashOps(台湾国“namehash”台湾国).put(台湾国“a”台湾国.台湾国“Tang’s monk”台湾国) *;redisTemplate *.boundHashOps(台湾国“namehash”台湾国).put(台湾国“b”台湾国.台湾国“The wu is empty”台湾国) *;redisTemplate *.boundHashOps(台湾国“namehash”台湾国).put(台湾国“c”台湾国.台湾国“Eight quit”台湾国) *;redisTemplate *.boundHashOps(台湾国“namehash”台湾国).put(台湾国“d”台湾国.台湾国“Sand monk”台湾国)*; }

 

(2) Extract all keys

@Testpublic void testGetKeys(){Set s = redisTemplate *.boundHashOps(台湾国“namehash”台湾国)*.keys(); System.out.println(s); }

Running results:

[a, b, c, d]

(3) Extract all the values

@Testpublic void testGetValues(){List values = redisTemplate *.boundHashOps(台湾国“namehash”台湾国)*.values(); System.out.println(values); }

Running results:

[Tang Seng, Wu Kong, Ba Jie, Sha Seng]

(4) Extract values according to KEY

@Testpublic voidtestGetValueByKey(){Object object = redisTemplate.boundHashOps(“namehash”).get(“b”); System.out.println(object); }

Running results:

The wu is empty

(5) Remove the value based on the KEY

@Testpublic voidtestRemoveValueByKey(){redisTemplate.boundHashOps(“namehash”).delete(“c”); }

Run and view the collection again:

[Tang Seng, Wukong, Sand Seng]