spring RedisTemplate用法
2024-09-08 18:19:29
1.maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
2.spring的xml配置
<bean id="jedisFactory" class="work.cache.redis.JedisPoolConnectionFactory"
destroy-method="close">
<constructor-arg ref="jedisPoolConfig" />
<constructor-arg value="127.0.0.1" />
<constructor-arg value="6379" />
</bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true"/>
</bean> <bean id="springRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
2.2 将bean传到业务类中
3 使用redisTemplate
springRedisTemplate.opsForValue().set(String.valueOf(report.getId()) +InstructionUtil.IsuSuffixRedisKey.ISU_HEART_BEAT.getKey(),1,80,TimeUnit.SECONDS);
RedisTemplate依赖:import org.springframework.data.redis.core.RedisTemplate;
参考地址:https://blog.csdn.net/liang_love_java/article/details/50497281
最新文章
- NodeJS写个爬虫,把文章放到kindle中阅读
- 一个Java递归删除目录的方法
- 大话JSON之Gson解析JSON
- eclipse的历史版本及下载
- Markdown中的缩进
- php 批量生成html、txt文件
- 解决Bootstrap 附加导航(Affix)的问题和使用时若干注意事项
- Platform Invoke
- apache2.2 + tomcat6 整合以及集群配置整理
- RHEL Server 6.3下MySQL5.5.25a源码安装
- RedHat7下PostGIS源码安装
- PHP生成图片验证码、点击切换实例
- Web前端 web的学习之路2
- 详解Transformer模型(Atention is all you need)
- JS实现抽奖(方形)
- 读书笔记(chapter17)
- Maya中输出nuke脚本的方法
- Mac svn使用学习-3-客户端调用服务端简单例子
- Spring3的表达式语言
- iOS开发之解决CocoaPods中“.h”头文件找不到的问题,简单粗暴的方法
热门文章
- Sql Server中order by对varchar类型排序结果不对
- Java学习笔记:2022年1月13日(其一)
- 从0到1手把手实现vite
- 高并发环境下3种方式优化Tomcat性能
- 初始rust
- Object类的toString方法-Object类的equas方法
- 基于APIView写接口
- XMind 2022 Win/macOS 使用教程
- VS 管理控制台提示ScriptHalted
- git操作出现 error: The following untracked working tree files would be overwritten by ...