In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What are the differences between redis and jedis? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!
The integration of redis and spring is generally divided into spring-data-redis integration and jedis integration. First look at the difference between the two.
1. The dependency of citation is different:
The dependencies used by spring-data-redis are as follows:
Org.springframework.data spring-data-redis 1.8.9.RELEASE
The dependencies used by jedis are as follows:
Redis.clients jedis 2.9.0 jar compile
2. Different ways of managing jedis instances and operating redis services:
Spring-data-redis:
It is managed through org.springframework.data.redis.connection.jedis.JedisConnectionFactory, that is, through factory class management, and then through the configured template bean, the redis service is operated. The code snippet is filled with a large number of template snippets that have nothing to do with business, which are redundant and difficult to maintain, such as the following code:
Protected RedisTemplate redisTemplate; public void saveUser (User user) {redisTemplate.execute (new RedisCallback () {@ Override public Object doInRedis (RedisConnection connection) throws DataAccessException {connection.set (redisTemplate.getStringSerializer (). Serialize ("user.uid." + user.getId ()), redisTemplate.getStringSerializer () .serialize (user.getName ()); return null;}})) } public User getUser (long id) {return redisTemplate.execute (new RedisCallback () {@ Override public User doInRedis (RedisConnection connection) throws DataAccessException {byte [] key = redisTemplate.getStringSerializer (). Serialize ("user.uid." + id); if (connection.exists (key)) {byte [] value = connection.get (key); String name = redisTemplate.getStringSerializer (). Deserialize (value) User user = new User (); user.setName (name); user.setId (id); return user;} return null;}};}
RedisTemplate introduction
Spring encapsulates RedisTemplate objects to perform various operations on redis, and it supports all redis native api. Several common interface methods are provided in RedisTemplate, which are:
Private ValueOperations valueOps;private ListOperations listOps;private SetOperations setOps;private ZSetOperations zSetOps
Operations on five data structures are defined in RedisTemplate
RedisTemplate.opsForValue (); / / operation string redisTemplate.opsForHash (); / / operation hashredisTemplate.opsForList (); / / operation listredisTemplate.opsForSet (); / / operation setredisTemplate.opsForZSet (); / / orderly operation setStringRedisTemplate and RedisTemplate
The relationship between the two is that StringRedisTemplate inherits RedisTemplate.
The data of the two are not in common; that is, StringRedisTemplate can only manage the data in StringRedisTemplate, and RedisTemplate can only manage the data in RedisTemplate.
There are two kinds of serialization strategies adopted by SDR by default, one is the serialization strategy of String and the other is the serialization strategy of JDK.
StringRedisTemplate defaults to the serialization strategy of String, and the saved key and value are serialized and saved using this policy.
RedisTemplate defaults to the serialization strategy of JDK, and the saved key and value are serialized and saved using this policy.
Jedis mode:
Manage through redis.clients.jedis.JedisPool, that is, manage through the pool, obtain the jedis instance through the pool object, and then directly operate the redis service through the jedis instance, eliminating redundant codes that have nothing to do with business, such as the following code snippet:
Private JedisPool jedisPool;public String save (String key,String val) {Jedis jedis = jedisPool.getResource (); return jedis.set (key, val);}
The change from factory class to pool is equivalent to the mybatis connection MySQL side change is the same, the code becomes simpler and easier to maintain. Jedis uses apache commons-pool2 to manage the Jedis resource pool, so a very important parameter when defining JedisPool is the resource pool GenericObjectPoolConfig, which is used as follows, in which there are many parameters for resource management and usage.
Parameter description
JedisPool ensures that resources are within a controllable range and provides thread safety, but a reasonable GenericObjectPoolConfig configuration can protect applications from using Redis. Some of its important parameters are described and recommended below:
In the current environment, Jedis connections are resources, and JedisPool manages Jedis connections.
1. Resource setting and usage
MaxTotal: maximum number of connections in the resource pool; default: 8. Recommendations for setting see the following section.
MaxIdle: the maximum number of idle connections allowed by the resource pool; default value: 8; use suggestions: set recommendations see the following section
MinIdle: the resource pool ensures the minimum number of idle connections; default value: 0; use recommendations: set recommendations see the following section
BlockWhenExhausted: whether the caller should wait when the resource pool is exhausted. The following maxWaitMillis will take effect only if it is true; default value: true; use suggestion: recommended default value
MaxWaitMillis: when the resource pool connection is exhausted, the maximum wait time of the caller (in milliseconds)-1: never times out; use suggestion: default value is not recommended
TestOnBorrow: whether to do connection validity test (ping) when borrowing connections from the resource pool, invalid connections will be removed; default value: false; recommendations: it is recommended to set it to false (the cost of one more ping) when the traffic is heavy.
TestOnReturn: whether to do connection validity test (ping) when returning the connection to the resource pool, the invalid connection will be removed; default value: false; recommendation: it is recommended to set it to false (the cost of one more ping) when the traffic is heavy.
JmxEnabled: whether to enable jmx monitoring, which can be used for monitoring; default value: true; use suggestion: it is recommended to enable it, but the application itself should be enabled.
2. Idle resource monitoring
Idle Jedis object detection is completed by a combination of the following four parameters, and testWhileIdle is the switch for this function.
TestWhileIdle: whether to enable free resource monitoring; default value: false; suggestion: true
TimeBetweenEvictionRunsMillis: detection period of free resources (in milliseconds); default value:-1: no detection; use suggestion: recommended setting, cycle choice, or you can use the configuration in the following JedisPoolConfig by default
MinEvictableIdleTimeMillis: minimum idle time (in milliseconds) of resources in the resource pool. After reaching this value, idle resources will be removed; default value: 1000 60 30 = 30 minutes. Use suggestion: you can use most of the default values according to your own business decision, or you can consider using the configuration in the following JeidsPoolConfig
NumTestsPerEvictionRun: the number of samples per time when doing idle resource detection; default value: 3; suggestions: fine-tune according to the number of connections applied by yourself. If set to-1, all connections are monitored for idle.
Thank you for reading! After reading the above, do you have a general understanding of the difference between redis and jedis? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.
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.