侯体宗的博客
  • 首页
  • Hyperf2.1版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Spring整合redis

Redis  /  管理员 发布于 2年前   191
pom.xml
<dependencies><!-- Spring Data - Redis Library --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.4.0.RELEASE</version></dependency><!-- Jedis Driver Library --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.6.0</version></dependency><!-- Spring Core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.1.RELEASE</version></dependency>        <!-- spring-data-redis --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.4.1.RELEASE</version></dependency></dependencies>


beans.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- Scanning the Spring Beans --><context:component-scan base-package="com.ljn.beans"></context:component-scan>    <bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:redis.properties</value>            </list>        </property>    </bean><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="${redis.pool.maxTotal}" /><property name="maxIdle" value="${redis.pool.maxIdle}" /><property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /></bean><!-- Redis Connection Factory --><bean id="jedisConnFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.ip}" /><property name="port" value="${redis.port}" /><property name="poolConfig" ref="jedisPoolConfig" /></bean><!-- Redis Template --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"p:connection-factory-ref="jedisConnFactory" /></beans>


redis.properties
redis.pool.maxTotal=1024redis.pool.maxIdle=200redis.pool.maxWaitMillis=3000redis.pool.testOnBorrow=trueredis.ip=10.75.201.3redis.port=63790


注意,旧版本的写法:

redis.pool.maxActive=1024

redis.pool.maxIdle=200

redis.pool.maxWait=1000

redis.pool.testOnBorrow=true

redis.ip=10.11.20.140

redis.port=6379

代码:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;@Componentpublic class RegistrationBean {@Autowiredprivate RedisTemplate<String,User> redisTemplate;public RedisTemplate<String, User> getRedisTemplate() {return redisTemplate;}public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {this.redisTemplate = redisTemplate;}}


  RegistrationBean bean = (RegistrationBean)context.getBean("registrationBean");// Persisting Inside the Hash User objectbean.getRedisTemplate().opsForHash().put("{a}UserA", user.hashCode(),user);// Retrieving the User object from the Redis by using the suggested keyUser x = (User)bean.getRedisTemplate().opsForHash().get("{b}UserA", user.hashCode());System.out.println(x.getFullName());

注意到上面的getRedisTemplate().opsForHash().set()方法,第一个参数是key本身,第二个参数是object的hashcode,第三个参数是object本身。
例如可以写代码测试:

        user.setFullName("ljn");        bean.getRedisTemplate().opsForHash().put("{b}UserA", user.hashCode(),user);        // Retrieving the User object from the Redis by using the suggested key        User y = (User)bean.getRedisTemplate().opsForHash().get("{b}UserA", user.hashCode());        System.out.println(y.getFullName());

这个时候的key跟前面的一样,但取得的value不一样,为修改后的值,getFullName输出:ljn


  • 上一条:
    keepalived实现redis主从高可用
    下一条:
    redis批量删除带空格的key
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • TP(3/5)
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • redis实现分页(0个评论)
    • Redis应用场景限流算法之一,计数器算法代码(0个评论)
    • 全网不一定最好的一篇Redis面试题解答详解,有补充希望您在底部留言(0个评论)
    • Redis为什么这么快(1个评论)
    • 近期文章
    • mysql性能优化之8种常见SQL错误用法(0个评论)
    • Laravel 9.13版本发布(0个评论)
    • beego+GeoLite2免费数据库获取ip地址经纬度等定位归属信息(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • PHP + Memcache实现简单的统计当前在线人数功能(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • go语言中使用Signbit()函数判断一个整数是正数或负数(0个评论)
    • 删库跑路之一链家程序员删除公司9TB数据被判7年,望各大码农警之!(0个评论)
    • Laravel角色和权限:拦截器Gates和策略Policies的解释(0个评论)
    • Laravel 9.12版本发布(0个评论)
    • 近期评论
    • 阿凡达123 在

      golang 怎么做热更新中评论 也可以看看这个:https://github.com/edwingeng/hot..
    • 博主 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..
    • GGGGGGGGG 在

      layui框架常用输入框介绍中评论 写的很好解决问题..
    • 路过的靓仔 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..
    • 激光豆芽 在

      为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客