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

Spring整合redis

Redis  /  管理员 发布于 3年前   280
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
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Redis集群之主从复制错误:NOAUTH Authentication required解决方式(0个评论)
    • redis数据同步工具redis-shake之两服务器之间数据实时同步案例(0个评论)
    • redis中INCR、SETNX、SET三种加锁方式详解(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • redis实现分页(0个评论)
    • 近期文章
    • 如何优雅处理async await错误推荐:await-to-js库(0个评论)
    • lodash工具库(0个评论)
    • 在Laravel项目中使用中间件方式统计用户在线时长功能代码示例(0个评论)
    • 在Laravel中构建业务流程模型(0个评论)
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    Top

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

    侯体宗的博客