Spring Data Operations Redis detailed
Redis is a NoSQL database that stores data in the form of Key-value, where data can exist in memory or persisted to the file system. Spring data is a good package for Redis and is very handy to use. Redis is an open-source (BSD-licensed), in-memory data structure storage system that can be used as a database, cache, and message middleware. It supports multiple types of data structures such as strings (strings), hashes (hashes), lists (lists), collections (sets), ordered collections (sorted sets) and range queries, bitmaps, hyperloglogs and geospatial (GEOSP atial) index RADIUS query. Redis has built-in replication (replication), LUA scripting (LUA scripting), LRU driver events (LRU eviction), transactions (transactions), and different levels of disk persistence (persistence), and Redis Sentinel (Sentinel) and automatic partitioning (Cluster) provide high availability (HI availability).
1. System configuration, if you are developing with Maven, simply add the following configuration to the Pom.xml file.
<Dependencies> <Dependency> <groupId>Org.springframework.data</groupId> <Artifactid>Spring-data-redis</Artifactid> <version>1.8.1.RELEASE</version> </Dependency></Dependencies>
<id= "Jedisconnfactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory " p:use-pool=" true "/><id=" Redistemplate " class = "Org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref= "jedisconnfactory"/>
2. The Redis template encapsulates the following actions for different requirements classifications.
Opsforvalue ()-Operations for working with entries have simple valuesopsforlist ()-Operations for working with entries Have List Valuesopsforset ()-Operations for working with entries have set Valuesopsforzset ()-Operations for Workin G with entries have Zset (sorted set) Valuesopsforhash ()-Operations for working with entries have hash Valuesboundva Lueops (k)-Operations for working and simple values bound to a given keyboundlistops (k)-Operations for working with Li St values bound to a given keyboundsetops (k)-Operations for working with set values bound to a given keyboundzset (k)-O Perations for working with Zset (sorted set) values bound to a given keyboundhashops (K)-Operations for working with Hash Values bound to a given key
3. Typical Operation example
3.1 Redis template injection, which can be injected either directly or in OPS, is illustrated in the following example.
Public classExample {//inject the actual template@AutowiredPrivateRedistemplate<string, string>template; //inject the template as Listoperations//can also inject as Value, Set, Zset, and Hashoperations@Resource (name= "Redistemplate") PrivateListoperations<string, string>Listops; Public voidAddlink (String userid, url url) {listops.leftpush (userid, Url.toexternalform ()); //or use template directlytemplate.boundlistops (userId). Leftpush (Url.toexternalform ()); }}
3.2 Bound Series Operation example, the advantage of the bound series operation is that it only needs to be bound once, and then you can do a series of operations, the code is very refined.
boundlistoperations<string, product> mangoops = Redis.boundlistops ("Solidmango"); = Mangoops.rightpop (); Mangoops.rightpush (PRODUCT1); Mangoops.rightpush (PRODUCT2); Mangoops.rightpush (PRODUCT3);
3.3 Serializer Configuration example, the key and value are typically persisted in different ways, as in the following example, key is persisted using string, and value is persisted using the Jackson format.
@Bean Public Redistemplate<string, cart> redistemplate (redisconnectionfactory RCF) { redistemplate<String , cart> Redis = new redistemplate<string, cart>(); Redis.setconnectionfactory (RCF); Redis.setkeyserializer (new Stringredisserializer ()); Redis.setvalueserializer ( thenew jackson2jsonredisserializer<product> (Product. Class)); return Redis;}
Summarize
In this paper, the configuration and development of the spring data operation Redis is explained in detail, the configuration section gives the specific configuration, the code example part of three kinds of situations give a specific solution, I hope to help you.
Spring Data Operations Redis detailed