Spring Data redis--Quick Start

Source: Internet
Author: User
Tags assert connection pooling

Environmental requirements: Redis 2.6 and above, javase 8.0 and above;

First, Spring Data Redis Introduction

Spring-data-redis is part of spring and provides a simple configuration to access the Redis service in the srping application, with a highly encapsulated reids underlying development package (Jedis, Jredis, and RJC). Redistemplate provides various operations, exception handling, and serialization of Redis, supports publishing subscriptions, and implements the Spring 3.1 cache.
Spring-data-redis provides the following features for Jedis:?

1. Connection pooling Automatic Management provides a highly encapsulated "Redistemplate" class.

2. Encapsulates a large number of APIs in the Jedis client, encapsulating the same type of operation as a operation interface

    • ? Valueoperations: Simple key-value pair operation String?
    • Setoperations:set type data operation set?
    • Zsetoperations:zset type data operation sortedset---->zset?
    • Hashoperations: Hash of data manipulation for hash type?
    • Listoperations: List of data operations for list type

Ii. examples of Getting Started

1. Environment construction

Selecting a Redis dependency using the Springboot build project

2. Configuring Redis

To change application.properties to APPLICATION.YML format

  1  spring:   2   Redis:   3   database:0   4   Host:localhost   5   Port:6379 6   password:   7   Jedis:   8< /span>  pool:   9   Max-active:8  10   Max-idle:811  min-idle:0 

3. Testing the relevant API in Test Springdataredisdemoapplicationtests.java

1  PackageCom.cenobitor.spring_data_redis_demo;2 3 ImportOrg.junit.Assert;4 Importorg.junit.Test;5 ImportOrg.junit.runner.RunWith;6 Importorg.springframework.beans.factory.annotation.Autowired;7 Importorg.springframework.boot.test.context.SpringBootTest;8 Importorg.springframework.data.redis.core.RedisTemplate;9 ImportOrg.springframework.test.context.junit4.SpringRunner;Ten Importjava.util.List; One ImportJava.util.Set; A  -@RunWith (Springrunner.class) - @SpringBootTest the  Public classspringdataredisdemoapplicationtests { -  - @Autowired -     Privateredistemplate redistemplate; +  - @Test +      Public voidcontextloads () { A     } at  -     /** - * Worth the action -      */ - @Test -      Public voidSetValue () { inRedistemplate.boundvalueops ("name"). Set ("Redis")); -     } to  + @Test -      Public voidGetValue () { theString str = (string) redistemplate.boundvalueops ("name"). get (); * System.out.println (str); $ assert.assertnotnull (str);Panax Notoginseng     } -  the     /** + * operation of set type A      */ the @Test +      Public voidSetsetvalue () { -Redistemplate.boundsetops ("NameSet"). Add ("Caocao")); $Redistemplate.boundsetops ("NameSet"). Add ("Sun Quan")); $Redistemplate.boundsetops ("NameSet"). Add ("Liu Bei")); -     } - @Test the      Public voidGetsetvalue () { -Set NameSet = Redistemplate.boundsetops ("NameSet"). Members ();WuyiSystem.out.println (NameSet);//[Liu Bei, Sun Quan, Cao] the     } -     //to delete an element in a collection Wu @Test -      Public voidDeletesetvalue () { AboutLong remove = Redistemplate.boundsetops ("NameSet"). Remove ("Liu Bei"); $ System.out.println (remove); -Assert.assertequals ("1", remove); -     } -     //Delete Entire collection A @Test +      Public voidDeleteset () { theBoolean NameSet = Redistemplate.delete ("NameSet"); -Assert.assertequals (true, nameset); $     } the  the     /** the * List Type operation the      */ -     //Right Stack: Post-add objects behind in @Test the      Public voidsetListValue1 () { theRedistemplate.boundlistops ("Namelist1"). Rightpush ("Liu Bei")); AboutRedistemplate.boundlistops ("Namelist1"). Rightpush ("Guan Yu")); theRedistemplate.boundlistops ("Namelist1"). Rightpush ("Zhang Fei")); the     } the @Test +      Public voidgetListValue1 () { -List List = Redistemplate.boundlistops ("Namelist1"). Range (0, 1); theSYSTEM.OUT.PRINTLN (list);//[Liu Bei, Guan Yu, Zhang Fei]Bayi     } the     //left pressure stack: After the addition of the object in the front row the @Test -      Public voidsetListValue2 () { -Redistemplate.boundlistops ("Namelist2"). Leftpush ("Liu Bei")); theRedistemplate.boundlistops ("Namelist2"). Leftpush ("Guan Yu")); theRedistemplate.boundlistops ("Namelist2"). Leftpush ("Zhang Fei")); the     } the @Test -      Public voidgetListValue2 () { theList List = Redistemplate.boundlistops ("Namelist2"). Range (0, 1); theSYSTEM.OUT.PRINTLN (list);//[Zhang Fei, Guan Yu, Liu Bei] the     }94     //querying a collection for an element the @Test the      Public voidSearchlistbyindex () { thestring s = (string) redistemplate.boundlistops ("Namelist1"). Index (1);98System.out.println (s);//Guan Yu About     } -     //remove an element from a collection101 @Test102      Public voidRemovelistbyindex () {103Redistemplate.boundlistops ("Namelist1"). Remove (1, "Guan Yu");104     } the 106     /**107 * Hash type operation108      */109 @Test the      Public voidSethashvalue () {111Redistemplate.boundhashops ("Namehash"). Put ("A", "Tang Monk")); theRedistemplate.boundhashops ("Namehash"). Put ("B", "Goku"));113Redistemplate.boundhashops ("Namehash"). Put ("C", "Eight commandments"); theRedistemplate.boundhashops ("Namehash"). Put ("D", "Sand Monk")); the     } the @Test117      Public voidGethash () {118         //extract all the keys119Set s = redistemplate.boundhashops ("Namehash"). Keys (); -System.out.println (s);//[A, B, C, d]121         //extract all the values122List values = Redistemplate.boundhashops ("Namehash"). values ();123System.out.println (values);//[Tang Monk, Wukong, Ba, Sha Monk]124         //extract values based on key theString str = (string) redistemplate.boundhashops ("Namehash"). Get ("B");126System.out.println (str);//Wu Empty127     } -     //remove values from key129 @Test the      Public voidRemovehashbykey () {131Redistemplate.boundhashops ("Namehash"). Delete ("C")); the     }133}

Spring Data redis--Quick Start

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.