Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the ways in which Java operates Redis

Shulou Source: shulou.com Published: 2022-05-31 11:11:36 10月16日 Update

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!

Tags: Sequence test configuration way problem content more knowledge project learning output practicality success success next two garbled at the same time dilemma reality. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux vpn Shulou Information OPPO Reno