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. ...
随机推荐
- [No0000196]一文读懂Java 11的ZGC为何如此高效
导读:GC是大部分现代语言内置的特性,Java 11 新加入的ZGC号称可以达到10ms 以下的 GC 停顿,本文作者对这一新功能进行了深入解析.同时还对还对这一新功能带来的其他可能性做了展望.ZGC ...
- git迁移
git迁移 项目开发的不同阶段可能要使用不同的git仓库,有时需要迁移. git有很好的方法,只需要几个命令 目标: 我们需要把代码从 http://a.com/projectA.git 迁移到 ht ...
- 洛谷P4092 [HEOI2016/TJOI2016]树 并查集/树链剖分+线段树
正解:并查集/树链剖分+线段树 解题报告: 传送门 感觉并查集的那个方法挺妙的,,,刚好又要复习下树剖了,所以就写个题解好了QwQ 首先说下并查集的方法趴QwQ 首先离线,读入所有操作,然后dfs遍历 ...
- mixpanel umeng talkingdata
市面上可以比较容易的接触到的实时大数据用户行为分析系统有很多,比如国外有著名的Mixpanel.Localytics.Google,国内有TalkingData.这些公司都提供基于云的大数据分析系统, ...
- 求职季全攻略:优秀的UX设计师作品集该如何准备?
这是一篇关于如何制作杰出的UX设计师作品集,看了下文,我们在2019年冲冲冲~制作出自己的作品集! 转自墨刀公众号. Paul Rand 曾经说:“设计很简单,因而也复杂.” 也许你已经掌握了所有的U ...
- Linux安装 centos 7 最小化 安装
我用的是vmware 15 ,我将用图片的形式带领大家安装,没有说到的地方为默认选项 最好把文件放到一个单独的文件夹中 简单密码点击两次DONE 即可,虚拟机密码不必那么复杂,工作环境请设 ...
- JSONModel(I)
JSONModel使用简介 JSONModel 只需要将你的 model 类继承自 JSONModel ,而同时 model 中的属性名又恰巧可以和 JSON 数据中的 key 名字一样的话,那么非常 ...
- (Review cs231n) Optimized Methods
Mini-batch SGD的步骤: 1.Sample a batch of data 2.Forward prop it through the graph,get loss 3.backprop ...
- java微信开发之地图定位
页面代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- GoldenGate12.3中新增的Parallel Replicat (PR)介绍
Parallel Replicat介绍 在OGG 12.3.0.1中新增的一项特性parallel replicat(并行投递),相对于传统的投递和集成投递(integrated replicat), ...