Redis introduction (6)

Source: Internet
Author: User
Redis Introduction
Redis is a fast non-relational database that can store mappings between keys and five different types of values ), you can persistently store the key-value pairs stored in the memory to the hard disk. You can use the replication feature to expand the read performance, or use client fragments to expand the write performance. To achieve high performance, redis uses a memory (in-memory) dataset (Dataset ). Based on the scenario, you can dump the dataset to the disk at intervals, or append each command to the log for persistence. Persistence can also be disabled. If you only need a network-based memory cache with rich functions.
Supported data types include:
String (string) Hash (hash) List (list) Set (SET) zset (sorted set: sorted set)
Advantages:
Redis's advantages include its speed, its support for rich data types, its atomicity of operations, and its versatility: A, high performance, it can execute about 100,000 sets and about 100,000 get operations per second. B. A wide range of data types. redis provides native support for most data types that most developers already know, which makes various problems easy to solve. C. atomicity. Because all redis operations are atomic, multiple clients will concurrently access a redis server to obtain the same update value. D. Rich features. redis is a multi-utility tool with many application scenarios, including cache and Message Queue (redis native supports publishing/subscription) and short-term application data (such as Web sessions and web page hit count.
Spring-boot-starter-data-redis
Spring boot provides a component package integrated with redis: Spring-boot-starter-data-redis. Spring-boot-starter-data-redis depends on spring-data-redis and Jedis. A. Jedis: Jedis is the Java client implementation of redis and is the officially recommended Java client. It encapsulates various operations on redis, and supports transactions, pipelines, and distributed features implemented by Jedis. B. Spring data: Spring data is a major project in the Spring framework. It aims to simplify the construction of data access based on Spring framework applications, this includes non-relational databases, map-Reduce frameworks, and cloud data services. It also supports access to relational databases. C. Spring data redis: Spring data redis is a major module in the spring Data Project. It implements highly encapsulated Jedis client APIs, making redis operations more convenient. Relationship between them: Jedis-> spring data redis-> spring data-> spring-boot-starter-data-redis, so spring data redis and Jedis have functions, all spring-boot-starter-data-redis instances are available.
Case study: Pom. xml ***** we use version 1.5.8
    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.8.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>        </dependency>        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.6</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <optional>true</optional>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <configuration>                    <fork>true</fork>                </configuration>            </plugin>        </plugins>    </build>
User class:
Public class user implements serializable {private long ID; private string username; private string password; private string email; private string nickname; private string regtime; // The setter and getter methods are omitted}
Redisconfig class:
@ Configuration @ enablecaching // enable public class redisconfig extends cachingassumersupport {@ bean public keygenerator () {return New keygenerator () {@ override public object generate (Object Targe, method, object... params) {stringbuilder sb = new stringbuilder (); sb. append (targe. getclass (). getname (); sb. append (method. getname (); For (Object OBJ: Params) {sb. append (obj. tostring ();} return sb. tostring () ;}}@ bean public cachemanager (redistemplate) {rediscachemanager RCM = new rediscachemanager (redistemplate); RCM. setdefaultexpiration (60); Return RCM ;}}
Application. Properties
# Redis database index (0 by default) spring. redis. Database = 0 # redis server address spring. redis. Host = 192.168.0.71 # redis server connection end? Port spring. redis. Port = 6379 # redis server connection password (blank by default) spring. redis. Password = # What is the maximum connection pool? Large number of connections (enable? Negative table? No restrictions.) spring. redis. Pool. Max-active = 8 # What is the maximum connection pool? Large blocking wait time (enable? Negative table? No restrictions.) spring. redis. Pool. Max-Wait =-1 # What is the most in the connection pool? Large idle connection spring. redis. Pool. Max-idle = 8 # What is the most in the connection pool? Small idle connection spring. redis. Pool. Min-idle = 0 # connection timeout (milliseconds) spring. redis. Timeout = 10000
Testredistemplate test class:
@ Runwith (springrunner. class) @ springboottest public class testredistemplate {@ autowired private redistemplate; @ test public void teststring () {redistemplate. opsforvalue (). set ("kid", "Doudou"); assert. assertequals ("Doudou", redistemplate. opsforvalue ();} // create a user object, put it in the cache, and then obtain @ test public void testobj () throws interruptedexception {user = new user ("[email protected]", "expire", "youknow", "expire", "2020"); valueoperations <string, user> operations = redistemplate. opsforvalue (); operations. set ("expire", user, 100, timeunit. microseconds); thread. sleep (1000); Boolean exists = redistemplate. haskey ("expire"); If (exists) {system. out. println ("exists is true");} else {system. out. println ("exists is false") ;}@ test public void testdelete () {valueoperations <string, user> operations = redistemplate. opsforvalue (); redistemplate. opsforvalue (). set ("deletekey", "ityouknow"); redistemplate. delete ("deletekey"); Boolean exists = redistemplate. haskey ("deletekey"); If (exists) {system. out. println ("exists is true");} else {system. out. println ("exists is false") ;}@ test public void testhash () {hashoperations <string, object, Object> hash = redistemplate. opsforhash (); hash. put ("hash", "you", "you"); string value = (string) hash. get ("hash", "you"); system. out. println ("hash value:" + value) ;}@ test public void testset () {string key = "set"; redistemplate. delete (key); setoperations <string, string> set = redistemplate. opsforset (); set. add (key, "it"); set. add (key, "you"); set. add (key, "you"); set. add (key, "know"); set <string> values = set. members (key); For (string V: values) {system. out. println ("Set Value:" + V) ;}@ test public void testsetmore () {setoperations <string, string> set = redistemplate. opsforset (); string key1 = "setmore1"; string key2 = "setmore2"; set. add (key1, "it"); set. add (key1, "you"); set. add (key1, "you"); set. add (key1, "know"); set. add (key2, "XX"); set. add (key2, "know"); set <string> diffs = set. difference (key1, key2); For (string V: diffs) {system. out. println ("diffs set value:" + V);} string key3 = "setmore3"; string key4 = "setmore4"; set. add (key3, "it"); set. add (key3, "you"); set. add (key3, "XX"); set. add (key4, "AA"); set. add (key4, "BB"); set. add (key4, "know"); set <string> unions = set. union (key3, key4); For (string V: unions) {system. out. println ("unions value:" + V) ;}@ test public void testzset () {string key = "zset"; redistemplate. delete (key); zsetoperations <string, string> zset = redistemplate. opsforzset (); zset. add (key, "it", 1); zset. add (key, "you", 6); zset. add (key, "know", 4); zset. add (key, "Neo", 3); set <string> zsets = zset. range (key, 0, 3); For (string V: zsets) {system. out. println ("zset value:" + V);} set <string> zsetb = zset. rangebyscore (Key, 0, 3); For (string V: zsetb) {system. out. println ("zsetb value:" + V );}}}

Redis introduction (6)

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.