当前位置:网站首页>Jedis 6 - Introduction and difference between redisson and jedis
Jedis 6 - Introduction and difference between redisson and jedis
2022-07-23 21:44:00 【Attacking polar bear】
1、redission
Official document address :
https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95
1.1 redission Distributed locks for
important :redission The most important is distributed lock 
1.2、java Use in RedissonClient operation redis ***
1.2.1、redission introduction

1.2.2、redission introduction + Basic operation of lock (Rlock A lock is a relocatable lock )

1.3、redis A single thread , Why do we need redission Do distributed locks

2、jedis
2.1、jedis and springboot Integration of
rely on :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bear</groupId>
<artifactId>redission-test</artifactId>
<version>1.0-SNAPSHOT</version>
<description> Yes redission Test of ,redission Used to connect to redis</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X Integrate redis what is needed common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
config layer :
package com.bear.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/** * < sketch > * < Detailed description > * * @author LiuShanshan * @version $Id$ */
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> 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 mode
template.setKeySerializer(redisSerializer);
//value serialize
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap serialize
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// Solve 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 the problem of garbled code ), Expiration time 600 second
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
application.properties To configure :
#Redis Server address
spring.redis.host=120.48.77.231
#spring.redis.password=123456
#Redis Server connection port
spring.redis.port=6379
#Redis Database index ( The default is 0)
spring.redis.database= 0
# Connection timeout ( millisecond )
spring.redis.timeout=1800000
# Maximum number of connections in connection pool ( Use a negative value to indicate that there is no limit )
spring.redis.lettuce.pool.max-active=20
# Maximum blocking waiting time ( A negative number means no limit )
spring.redis.lettuce.pool.max-wait=-1
# The maximum free connection in the connection pool
spring.redis.lettuce.pool.max-idle=5
# The smallest free connection in the connection pool
spring.redis.lettuce.pool.min-idle=0
controller layer :
package com.bear.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** * < sketch > * < Detailed description > * * @author LiuShanshan * @version $Id$ */
@RestController
@RequestMapping("/redisTest")
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping
public String testRedis() {
// Set value to redis
redisTemplate.opsForValue().set("name","lucy");
// from redis Get value
String name = (String)redisTemplate.opsForValue().get("name");
return name;
}
}
边栏推荐
猜你喜欢

Broadcast (broadcast)

CMake的学习

Cookie 和 Session

U++学习笔记 TSubclassOf()

Hezhou esp32c3 hardware configuration information serial port printout

Practice data Lake iceberg lesson 37 kakfa write the enfour, not enfour test of iceberg's icberg table

Basic knowledge of mobile phone testing

Kuberntes cloud native combat VI uses rook to build CEPH cluster

MySQL数据库索引

基于速度、复杂性等因素比较KernelSHAP和TreeSHAP
随机推荐
MySql的DDL和DML和DQL的基本语法
Union and union all of Hana SQL
How to implement desktop lyrics in pyqt
Cesium core class viewer viewer details
JS object array de duplication
SQL注入攻击
Scala programming (intermediate advanced experimental application)
prime_ series_ level-1
Compare kernelshap and treeshap based on speed, complexity and other factors
【数学建模暑期培训】配送中心选址问题
北大清华2022年在各地录取人数排名
基于速度、复杂性等因素比较KernelSHAP和TreeSHAP
U++ 学习笔记 控制物体Scale
jedis 6---redisson和jedis的入门和不同
Protocol buffers 的问题和滥用
Hezhou esp32c3 hardware configuration information serial port printout
A stack of digital robots were selected in Gartner's China AI market guide
VLAN comprehensive experiment
PCL error: error c2589 "(": "::" illegal mark on the right)
Cesium keyboard and mouse control camera roaming (source code + principle explanation)