First, introduce Maven dependencies
<! -- Redis dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>Copy the code
2. Configure Redis information
Spring: redis: host: 192.168.1.152 Port: 6870 Password: 2503 Database: 0Copy the code
Add Redis configuration class
@configuration public class RedisConfig {@autoWired private RedisTemplate RedisTemplate; @ Bean public RedisTemplate redisTemplateInit () {/ / set the serialization Key object instantiated RedisTemplate. SetKeySerializer (new StringRedisSerializer()); / / set the Value the instantiation of the object serialization redisTemplate. SetValueSerializer (new GenericJackson2JsonRedisSerializer ()); return redisTemplate; }}Copy the code
4. Use the RedisTemplate
@RestController @RequestMapping("/article") public class ArticleEntityController { /** * */ @autoWired Private RedisTemplate RedisTemplate; @ RequestMapping ("/list ") public String list () {/ / setting reids value redisTemplate opsForValue (). The set (" redis_key ", "in the redis values"); / / remove the redis value String redis_key = (String) redisTemplate. OpsForValue () get (" redis_key "); return redis_key; }}Copy the code
5. Common methods of RedisTemplate
. / / add a key/value redisTemplate opsForValue (). The set (" redis_key ", "in the redis values"); / / add a key/value and set the expiration time, have the atomic redisTemplate. OpsForValue (). The set (" redis_key ", "the values stored in redis", 20, TimeUnit. SECONDS); / / remove the redis value String redis_key = (String) redisTemplate. OpsForValue () get (" redis_key "); Boolean hasKey = redistemplate. hasKey("redis_key"); // Delete key Boolean delete = redistemplate. delete("redis_key"); / / order increase, the primary key can be used in distributed generation Long index = redisTemplate. OpsForValue () increment (" index "); // Execute lua script String key = "index"; String value = "UUID"; stringRedisTemplate.opsForValue().set(key, value); DefaultRedisScript redisScript = new DefaultRedisScript<>(); redisScript.setLocation(new ClassPathResource("lua/delete_key.lua")); redisScript.setResultType(Long.class); Long execute = stringRedisTemplate.execute(redisScript, (RedisSerializer) stringRedisTemplate.getKeySerializer(), (RedisSerializer) stringRedisTemplate.getKeySerializer(), Lists.newArrayList(key), value); System.out.println(" execute result: "+ execute);Copy the code
RedisTemplate and StringRedisTemplate
1. By default, the RedisTemplate adopts the SERIalization strategy of JDK. The key/value stored in the RedisTemplate will first serialize the data into byte array and then store the data in Redis
2. StringRedisTemplate uses the serialization strategy of String by default. The key/value stored in the String is stored in plain text, which is used when the data stored is a String
3. Also note that data stored with RedisTemplate must be retrieved with RedisTemplate, otherwise null will be obtained. StringRedisTemplate similarly.
@every gold digging friends, if you have any Java technology questions can privately chat myself (WX: haostudyDaydayUp), discuss progress together.