Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The function and usage of Spring Data Redis

Shulou Source: shulou.com Published: 2022-06-02 09:11:20 09月12日 Update

This article mainly introduces "the function and usage of Spring Data Redis". In the daily operation, I believe many people have doubts about the function and usage of Spring Data Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the function and usage of Spring Data Redis". Next, please follow the editor to study!

SpringDataRedis is a part of the Spring family, which provides access to redis services through simple configuration in Spring applications, highly encapsulates the underlying development package of redis, RedisTemplate provides various operations of redis, exception handling and serialization, supports distributed subscription, and implements spring3.1cache.

The functions provided by SpringDataRedis:

1. Automatic management of connection pool, providing a highly encapsulated RedisTemplate class

2. A large number of API in Jedis client are classified and encapsulated, and the same type of operation is encapsulated as operation interface.

ValueOperations: simple Kmurv operation

SetOperations:set type data operation

ZSetOperations:zset type data operation

HashOperations: data manipulation for map types

ListOperations: data manipulation for list types

Getting started case:

1. Import dependency

Org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-test

2. Create a startup class

@ SpringBootApplicationpublic class RedisApplication {public static void main (String [] args) {SpringApplication.run (RedisApplication.class);}}

3. Operation value type

@ RunWith (SpringRunner.class) @ SpringBootTestpublic class RedisTests {@ Autowired private RedisTemplate template; @ Testpublic void setValue () {template.boundValueOps ("name") .set ("lianbang.wu");} @ Testpublic void getValue () {String str = (String) template.boundValueOps ("name"). Get (); System.out.println (str);} @ Testpublic void deleteValue () {template.delete ("name") }}

4. Operation set type

@ Testpublic void setValue () {template.boundSetOps ("nameset"). Add ("Reader 1"); template.boundSetOps ("nameset"). Add ("Reader 2"); template.boundSetOps ("nameset"). Add ("Reader 3");} @ Testpublic void getValue () {Set members = template.boundSetOps ("nameset"). Members (); System.out.println (members) } @ Testpublic void deleteValue () {template.boundSetOps ("nameset") .remove ("Reader 1");} @ Testpublic void deleteAll () {template.delete ("nameset");}

5. Operate List

@ Test public void testSetValue () {template.boundListOps ("namelist1"). RightPush ("Reader 1"); template.boundListOps ("namelist1"). LeftPush ("Reader 2");} @ Test public void testGetValue () {List namelist1 = template.boundListOps ("namelist1"). Range (0,10); System.out.println (namelist1);} @ Test public void testSearechIndex () {Object namelist1 = template.boundListOps ("namelist1"). Index (1); System.out.println (namelist1) } @ Test public void testRemoveByIndex () {template.boundListOps ("namelist1") .remove (1, "Reader 1");}

6. Manipulate Hash type

@ Testpublic void testSetValue () {template.boundHashOps ("namehash"). Put ("a", "Reader 1"); template.boundHashOps ("namehash"). Put ("b", "Reader 2");} @ Testpublic void testGetKeys () {Set s = template.boundHashOps ("namehash"). Keys (); System.out.println (s);} @ Testpublic void testGetValue () {List values = template.boundHashOps ("namehash"). Values (); System.out.println (values) } @ Testpublic void testGetValueByKey () {Object o = template.boundHashOps ("namehash"). Get ("a"); System.out.println (o);} @ Testpublic void testRemoveValueByKey () {template.boundHashOps ("namehash"). Delete ("b"); at this point, the study on "the function and usage of Spring Data Redis" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Reader type function method usage data learning encapsulation more high degree help utility next big family customer client sequence underlying development package interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft MySQL Xiaomi Shulou Tech Info