How to integrate Lettuce redis in SpringBoot
This article mainly introduces the relevant knowledge of "how to integrate Lettuce redis in SpringBoot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to integrate Lettuce redis in SpringBoot" can help you solve the problem.
1. Add dependencies
Org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2
2. Add redis configuration
Spring: redis: host: * password:**** port: 6379 # connection timeout (milliseconds) timeout: 1000 # Redis there are 16 shards by default. Specific shards are configured here. Default is 0 database: 0 # connection pool configuration lettuce: pool: # maximum number of connections in connection pool (use negative values for no limit) default 8 max-active: 8 # connection pool maximum blocking wait time (use negative values for no limit) default-1 max-wait:-1 # maximum idle connections in connection pool default 8 max-idle: 8 # the minimum idle connection in connection pool defaults to 0 min-idle: 0
3. Implementation logic
@ Autowired private StringRedisTemplate stringRedisTemplate; @ Override public String testRedis () {ExecutorService executorService = Executors.newFixedThreadPool (1000); IntStream.range (0, 1000) .forEach (I-> executorService.execute (()-> stringRedisTemplate.opsForValue () .increment ("lcl", 1)); System.out.println ("lcl1=" + stringRedisTemplate.opsForValue () .get ("lcl")); stringRedisTemplate.opsForValue () .set ("lcl1", "val1"); String val1 = stringRedisTemplate.opsForValue () .get ("lcl1") System.out.println ("lcl1=" + val1); String key = "redis:test:demo1"; User user = new User (); user.setId (100L); user.setUsername ("U2"); user.setPassword ("p2"); stringRedisTemplate.opsForValue (). Set (key, JSON.toJSONString (user)); String valUser = stringRedisTemplate.opsForValue (). Get (key); System.out.println ("redis:test:demo1=" + valUser) User getUser = JSON.parseObject (valUser, User.class); System.out.println ("redis:test:demo1=" + getUser.getUsername () + "=" + getUser.getPassword ()); return null;}
Test results:
Because redis has String, list, set, zset, hash, geo and other types, not only the opsForValue () method is used, the specific corresponding methods are as follows:
OpsForValue: corresponding String (string)
OpsForZSet: corresponding ZSet (ordered set)
OpsForHash: corresponding to Hash (hash)
OpsForList: corresponding List (list)
OpsForSet: corresponding Set (collection)
OpsForGeo: corresponding GEO (geolocation)
This is the end of the introduction to "how to integrate Lettuce redis in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.