In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how SpringBoot integrates Redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
SpringBoot Integration Redis1, Overview
What is Redis?
Redis (Remote Dictionary Server), the remote dictionary service.
Is an open source ANSI C language to write, support the network, memory-based and persistent log-type, Key-Value database, and provide multiple languages of API.
Like memcached, data is cached in memory for efficiency. The difference is that redis will periodically write updated data to disk or modify operations to additional record files, and on this basis to achieve master-slave (master-slave) synchronization.
What can Redis do?
Memory storage, persistence, memory is lost immediately after power outage, so need persistence (RDB, AOF) high efficiency, for caching publish and subscribe system map information analysis timer, counter (eg: pageviews). ...
Characteristics
Multiple data types
Persistence
Cluster
Business
...
2. Test Redis
SpringBoot operation data, Spring-Data, jbdc, redis... ...
SpringData is a project as famous as SpringBoot!
Note: after SpringBoot2.x, the original jedis was replaced with lettuce
Jedis: the use of direct connection, multi-thread operation, is not safe, if you want to avoid unsafe, you need to use jedis pool connection pool! Like BIO mode
Lettuce: with netty, instances can be shared among multiple threads, and there is no thread unsafe situation! Can reduce thread data, more like NIO mode
Create a new project
Note:
View the underlying layer
Source code analysis:
@ Bean@ConditionalOnMissingBean (/ / if the component condition is not injected, we can define a redisTemplate to replace the default name = {"redisTemplate"}) public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {/ / default RedisTemplate does not have too many settings redis both need to be serialized! / / both generics are Object Object types, and we need to cast RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (redisConnectionFactory) later Return template;} @ Bean@ConditionalOnMissingBean / / since String is the most commonly used type in redis, a beanned public StringRedisTemplate stringRedisTemplate (RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate (); template.setConnectionFactory (redisConnectionFactory); return template;} is proposed separately.
1. Import dependency
2. Configure the connection
# SpringBoot all configuration classes have an automatic configuration class RedisAutoConfiguration# automatic configuration class is bound to a properties configuration file RedisProperties# configuration redisspring.redis.host=127.0.0.1spring.redis.port=6379spring.redis
3. Test!
Package com.kk;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisTemplate;@SpringBootTestclass Redis01SpringbootApplicationTests {@ Autowired private RedisTemplate redisTemplate @ Test void contextLoads () {/ * redisTemplate opsForValue operation string similar to String opsForList operation List similar to List opsForSet opsForHash opsForZSet opsForGeo opsForHyperLogLog except for basic operations Our commonly used methods can get the connection object of redis / / RedisConnection connection = redisTemplate.getConnectionFactory () .getConnection () directly through redisTemplate, such as transaction and basic CRUD * / /. / / connection.flushDb (); / / connection.flushAll (); redisTemplate.opsForValue () .set ("kk1", "kk2"); System.out.println (redisTemplate.opsForValue () .get ("kk1"));}}
3. Custom redisTemplate
First, build an entity class and test
User class
Package com.kk.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.stereotype.Component;import java.io.Serializable;@Component@Data@AllArgsConstructor@NoArgsConstructor// in the enterprise, all of our pojo serialize public class User implements Serializable {private String name; private int age;}
Test:
@ Testpublic void test () throws JsonProcessingException {/ / Real developers generally use json to pass the object User user = new User ("kk", 17); String jsonUser = new ObjectMapper () .writeValueAsString (user); / / this becomes a json object redisTemplate.opsForValue (). Set ("user", jsonUser); System.out.println (redisTemplate.opsForValue (). Get ("user"));} r = new ObjectMapper (). WriteValueAsString (user) / / this becomes a json object redisTemplate.opsForValue () .set ("user", jsonUser); System.out.println (redisTemplate.opsForValue () .get ("user"));}
= Note: if serialization is not implemented in the User class, it will report an error = =
Thank you for reading! This is the end of the article on "how to integrate SpringBoot with Redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.