In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how Java operates Redis". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Jedis operates Redis to create an idea project
Create a Maven project and import Jedis dependencies:
Redis.clients jedis 3.2.0 create the JedisDemo test class package com.vleus.jedis;import redis.clients.jedis.Jedis;/** * @ author vleus * @ date after importing the dependency 23:52 * / public class JedisDemo1 {public static void main (String [] args) {/ / create the Jedis object Jedis jedis = new Jedis ("192.168.37.139", 6379) / / Test whether the Jedis client can connect to Redis System.out.println (jedis.ping ());}}
The output PONG indicates a successful connection ~
Note:
If the Redis of the virtual machine is not connected to the virtual machine during the first test, there are two problems that need to be resolved if the exception that cannot be connected is reported:
First, kill the process of Redis, and then modify the configuration file used to start Reids.
Comment out the configuration of bind
Change the value of protected-mode to no at the same time
Step 2, turn off Linux's firewall (this is only for learning, don't do this in the production environment)
Systemctl stop firewalldsystemctl disable firewalld
Anyway, that's how I solved it.
Jedis operation Redis
1. Create an idea springboot project and introduce dependencies
Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 2.6.2 com.fasterxml.jackson.core jackson-databind 2.8.3
2. Create a RedisConfig configuration class
@ EnableCaching@Configurationpublic class RedisConfig {@ Bean public RedisTemplate redisTemplate (RedisConnectionFactory factory) {RedisTemplate template = new RedisTemplate (); RedisSerializer redisSerializer = new StringRedisSerializer (); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om); template.setConnectionFactory (factory) / / key serialization method template.setKeySerializer (redisSerializer); / / value serialization template.setValueSerializer (jackson2JsonRedisSerializer); / / value hashmap serialization template.setHashValueSerializer (jackson2JsonRedisSerializer); return template;} @ Bean public CacheManager cacheManager (RedisConnectionFactory factory) {RedisSerializer redisSerializer = new StringRedisSerializer (); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class) / / resolve the problem of query cache conversion exception ObjectMapper om = new ObjectMapper (); om.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (om) / / configure serialization (solve garbled problem). Expiration time: RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig () .entryTtl (Duration.ofSeconds) .serializeKeysWith (RedisSerializationContext.SerializationPair.fromSerializer (redisSerializer)) .serializeValuesWith (RedisSerializationContext.SerializationPair.fromSerializer (jackson2JsonRedisSerializer)) .serialCachingNullValues () RedisCacheManager cacheManager = RedisCacheManager.builder (factory) .cacheDefaults (config) .build (); return cacheManager;}} simple Controller test / * * @ author vleus * @ date 13:06 * / @ RestController@RequestMapping (value = "/ redisTest") public class RedisTestController {@ Resource private RedisTemplate redisTemplate @ GetMapping public String testRedis () {/ / set the value to redis redisTemplate.opsForValue () .set ("name", "jack"); / / get the value from redis String name = (String) redisTemplate.opsForValue () .get ("name"); return name;}} "how Java operates Redis" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.