博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jedis的使用及SpringBoot整合Redis
阅读量:3950 次
发布时间:2019-05-24

本文共 3307 字,大约阅读时间需要 11 分钟。

1、导入对应的依赖

redis.clients
jedis
3.2.0
com.alibaba
fastjson
1.2.62
commons-pool
commons-pool
1.6

事务的使用

public static void main(String[] args){
Jedis jedis = new Jedis("39.96.113.156", 6379);// 设置密码 jedis.auth("26993958a"); jedis.flushDB(); JSONObject jsonObject = new JSONObject(); jsonObject.put("hello","world"); jsonObject.put("name","kuangshen"); System.out.println(jedis.ping()); // 开启事务 Transaction multi = jedis.multi(); String result = jsonObject.toJSONString(); // jedis.watch(result) try {
multi.set("user1",result); multi.set("user2",result); multi.exec(); // 执行事务! } catch (Exception e) {
multi.discard(); // 放弃事务 e.printStackTrace(); } finally {
System.out.println(jedis.get("user1")); System.out.println(jedis.get("user2")); jedis.close(); // 关闭连接 } }

SpringBoot整合

SpringBoot 操作数据:spring-data jpa jdbc mongodb redis!

SpringData 也是和 SpringBoot 齐名的项目!
说明: 在 SpringBoot2.x 之后,原来使用的jedis 被替换为了 lettuce?
jedis : 采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接
池! 更像 BIO 模式
lettuce : 采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据
了,更像 NIO 模式
源码分析:

@Configuration(proxyBeanMethods = false)@ConditionalOnClass(RedisOperations.class)@EnableConfigurationProperties(RedisProperties.class)@Import({
LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })public class RedisAutoConfiguration {
@Bean @ConditionalOnMissingBean(name = "redisTemplate") // 我们可以自己定义一个 redisTemplate来替换这个默认的! public RedisTemplate
redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
// 默认的 RedisTemplate 没有过多的设置,redis 对象都是需要序列化! // 两个泛型都是 Object, Object 的类型,我们后使用需要强制转换
RedisTemplate
template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean // 由于 String 是redis中最常使用的类型,所以说单独提出来了一 个bean! public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }}

1.导入依赖

org.springframework.boot
spring-boot-starter-data-redis

2.配置连接

spring.redis.host=xxxxxxxspring.redis.port=xxxxxxxspring.redis.password=xxxxxxx

3.测试

@Test    public void contextLoads() {
// opsForValu操作字符串 redisTemplate.opsForValue().set("name","chenshengming"); String name = (String)redisTemplate.opsForValue().get("name"); System.out.println(name);// 返回Redis的链接对象 RedisConnection redisConnection=redisTemplate.getConnectionFactory().getConnection(); redisConnection.flushDb(); redisConnection.flushAll(); }

在这里插入图片描述

在这里插入图片描述
4.关于对象的保存:
在这里插入图片描述
5.我们来编写一个自己的 RedisTemplete

转载地址:http://emrwi.baihongyu.com/

你可能感兴趣的文章
已知前序遍历和中序遍历求二叉树
查看>>
已知后序遍历和中序遍历求二叉树
查看>>
使用最小花费爬楼梯 (LeetCode - 746)
查看>>
勾股数 (迅雷笔试题)
查看>>
平安夜杀手 (科大讯飞笔试题)
查看>>
计算器 (贝壳笔试题)
查看>>
Prime Path POJ - 3126 ( 素数+搜索)
查看>>
迷宫问题 POJ - 3984 ( 搜索 最短路 记录路径 )
查看>>
全排列 51Nod - 1384 ( 搜索dfs / STL - next_permutation函数 )
查看>>
Catch That Cow HDU - 2717 ( 搜索 )
查看>>
Oil Deposits HDU - 1241 ( 搜索DFS )
查看>>
2019 网易校园招聘---[小易的字典]
查看>>
1001 害死人不偿命的(3n+1)猜想 (15 分)
查看>>
1003 我要通过! (20 分)
查看>>
1004 成绩排名 (20 分)
查看>>
1005 继续(3n+1)猜想 (25 分)
查看>>
1006 换个格式输出整数 (15 分)
查看>>
1007 素数对猜想 (20 分)
查看>>
1008 数组元素循环右移问题 (20 分)
查看>>
1009 说反话 (20 分)
查看>>