当前位置:网站首页>jedis 6---redisson和jedis的入门和不同
jedis 6---redisson和jedis的入门和不同
2022-07-23 21:43:00 【进击的北极熊】
1、redission
官方文档地址:
https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95
1.1 redission的分布式锁
重要:redission中最重要的就是分布式锁
1.2、java中使用RedissonClient操作redis ***
1.2.1、redission入门

1.2.2、redission 入门 +锁的基本操作(Rlock锁是可重入锁)

1.3、redis是单线程,为啥还需要redission做分布式锁

2、jedis
2.1、jedis和springboot的整合
依赖:
<?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>对redission的测试,redission用于连接redis</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需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层:
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;
/** * <简述> * <详细描述> * * @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序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
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);
// 配置序列化(解决乱码的问题),过期时间600秒
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配置:
#Redis服务器地址
spring.redis.host=120.48.77.231
#spring.redis.password=123456
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database= 0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
controller层:
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;
/** * <简述> * <详细描述> * * @author LiuShanshan * @version $Id$ */
@RestController
@RequestMapping("/redisTest")
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping
public String testRedis() {
//设置值到redis
redisTemplate.opsForValue().set("name","lucy");
//从redis获取值
String name = (String)redisTemplate.opsForValue().get("name");
return name;
}
}
边栏推荐
- googletest
- SQLite database
- 剑指Offer第二版:字符串(简单)
- 分布式能源的不确定性——风速测试(Matlab代码实现)
- Interval DP chain stone merging
- High numbers | calculation of double integral 3 | high numbers | handwritten notes
- Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
- Modular development
- [attack and defense world web] difficulty four-star 12 point advanced question: confusion1
- uniapp使用canvas写环形进度条
猜你喜欢

Cluster chat server: Framework Design of model data layer and encapsulation of database code

Protocol buffers 的问题和滥用

Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy

Unity—3D数学-Vector3

Principle and implementation of hash table, unordered set and mapping

集群聊天服务器:数据库表的设计

寻找消失的类名

Be a professional software craftsman

Yushu A1 robot dog gesture control

Unity - 3D mathematics -vector3
随机推荐
js 对象数组去重
Flink principle and development summary (detailed)
实时监控Mysql数据库变化_进行数据同步_了解Canal_---Canal工作笔记001
基于速度、复杂性等因素比较KernelSHAP和TreeSHAP
Bisection function details
05_ UE4 advanced_ Material UV scaling
Chapter 2 Regression
Postgraduate entrance examination | advanced mathematics Chapter4 indefinite integral
【arxiv】第一次上传论文小记
Scala programming (elementary)
[isprint function determines whether characters can be output]
给定一个以数字组成的数组,实现输出id为数字,并且从小到大排序的name
At 12 o'clock on July 23, 2022, the deviation from the top of the line of love life hour appeared, maintaining a downward trend and waiting for the rebound signal.
scala编程(中级进阶实验应用)
Still have 1 requests outstanding when connection from slaveX/X.X.X.X:33202 is closed
SQL注入攻击
Openlayers instances advanced mapbox vector tiles advanced mapbox vector maps
What is Kai Niu? Excuse me, is it safe to open a stock account by mobile phone?
How to use cesium knockout?
How to get the worker's hat? Where is the worker's helmet?