Spring中使用Redis
普通使用Redis的方法很简单,前面的文章也有讲解,这篇文章主要就是讲解通过注解的方式实现Spring和Redis的整合。
这里我们创建了3个类:
1.Config 全局配置类,相当于xml配置文件
2.RedisTest 启动类,就一个main方法,同时初始化Spring容器
3.SpringStart 具体操作类,在这个类里面操作具体的Redis
pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
config
package cn.duanjt; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration
public class Config {
@Bean("poolConfig")
public JedisPoolConfig JedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(100);
config.setTestOnBorrow(true);
return config;
//return new JedisPool(config, "172.23.88.107", 6379);
} @Bean("jedisConnectionFactory")
public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig poolConfig){
JedisConnectionFactory factory=new JedisConnectionFactory(); factory.setHostName("172.23.88.107");
factory.setPort(6379);
factory.setPoolConfig(poolConfig);
factory.setUsePool(true);
return factory;
} @Bean("redisTemplate")
public RedisTemplate<String, Object> RedisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template=new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);//开启事务
return template;
} @Bean
public SpringStart SpringStart(){
return new SpringStart();
}
}
RedisTest
package cn.duanjt; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class RedisTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
SpringStart test=context.getBean(SpringStart.class);
test.start();
} }
SpringStart
package cn.duanjt; import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; public class SpringStart {
@Autowired
RedisTemplate<String, Object> redisTemplate; public void start() {
// 设置值
redisTemplate.opsForValue().set("age", 100);
// 获取值
System.out.println("String年龄"+redisTemplate.opsForValue().get("age")); Map<String, String> map = new HashMap<>();
map.put("age", "100");
map.put("name", "张三");
redisTemplate.opsForHash().putAll("stu:001", map); System.out.println("Hash姓名:"+redisTemplate.opsForHash().get("stu:001", "age"));
//redisTemplate.opsForSet();
//redisTemplate.opsForZSet();
}
}
1.注意最后的redisTemplate.opsForSet()和redisTemplate.opsForZSet(),Redis里面的不同类型对应了一个不同方法
2.开始启动的时候报了类没有找到的错误,后来排查是因为jedis和spring-data-redis的版本不兼容,后来将jedis修改为2.9.0之后完美解决。
Spring中使用Redis的更多相关文章
- 记自己在spring中使用redis遇到的两个坑
本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...
- spring中订阅redis键值过期消息通知
1.首先启用redis通知功能(ubuntu下操作):编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知): notify-keyspace-events Ex 或者登陆 ...
- 在Spring中使用Redis Lua脚本批量删除缓存
背景 之前分享了一篇利用lua脚本批量删除redis的key的文章.现在项目中我打算使用spring的缓存,而Spring缓存以前我是用ehcache来做实现的.没发现什么问题..这次我换成redis ...
- spring中添加redis缓存
1.单机版的添加 spring里面配置 <bean id="redisClient" class="redis.clients.jedis.JedisPool&qu ...
- 在SpringBoot中引入Redis
前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...
- Spring+Dubbo集成Redis的两种解决方案
当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...
- Spring下使用Redis
在Spring中使用Redis使用使用两个依赖包jedis.jar.spring-data-redis.jar 一下是Maven项目pom.xml添加依赖 <!--jedis.jar --> ...
- 浅析redis缓存 在spring中的配置 及其简单的使用
一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
RedisTemplate如何检查一个key是否存在? return getRedisTemplate().hasKey(key); 由一个问题,复习了一下redis 抄自: https://www. ...
随机推荐
- .NET Core 2.1 源码学习:看 SocketsHttpHandler 如何在异步方法中连接 Socket
在 .NET Core 2.1 中,System.Net.Sockets 的性能有了很大的提升,最好的证明是 Kestrel 与 HttpClient 都改为使用 System.Net.Sockets ...
- python描述符
class Type: def __init__(self, key, expect_type): self.key = key self.expect_type = expect_type def ...
- linux_rename命令用法
rename在man中的解释为: NAME rename - rename files SYNOPSIS rename [options] expression replacement file... ...
- 用java代码解决excel打开csv文件乱码问题
Java 读取csv文件后,再保存到磁盘上,然后直接用Excel打开,你会发现里面都是乱码. 贴上代码: public class Test { public static void main(S ...
- C和C指针小记(十八)-使用结构和指针-双向链表
1.双链表 1.1 双向链表的声明 在一个双链表中,每个节点都包含两个指针--指向前一个节点的指针和指向后一个节点的指针. 声明 typedef struct NODE { struct NODE * ...
- spark-sql中的DataFrame文件格式转储示例
SparkConf sparkConf = new SparkConf() // .setMaster("local") .setAppName("DataFrameTe ...
- POJ3461 Oulipo 字符串
正解:kmp/哈希 解题报告: 传送门! 这题其实就kmp板子,,,用来复习下kmp的太久没打了QAQ 所以kmp做法就不港了放个代码就是了QAQ #include<algorithm> ...
- PHP Echarts Ajax Json柱形图示例
<?php $server = '127.0.0.1'; $user = 'root'; $password = ''; $database = 'yiibaidb'; $conn = new ...
- [摘抄] Bezier曲线、B样条和NURBS
Bezier曲线.B样条和NURBS,NURBS是Non-Uniform Rational B-Splines的缩写,都是根据控制点来生成曲线的,那么他们有什么区别了?简单来说,就是: Bezier曲 ...
- windows更改Jupyter Notebook工作的目录
1.将打开的Jupyter Notebook程序关闭,然后找到桌面快捷方式,右键=>属性,然后把目标后面输入框最后的“%USERPROFILE%”这个参数去掉后,确定.否则之后做的其它修改无法生 ...