JaveWeb项目使用redis缓存,封装工具类,存储String,Map,List,Set类型数据。及其它个人初步见解

本文详细介绍了如何在JavaWeb项目中集成Redis,包括pom.xml的依赖配置,ApplicationContext-redis.xml和redis.properties的数据源设定。重点讲述了如何封装redisTemplate和Jedis工具类,实现String、Map、List、Set等数据类型的存取,并通过Debug进行测试。同时,通过可视化工具解析了不同存储结构的实质差异。
摘要由CSDN通过智能技术生成

第一步:依赖包导入(pom.xml)

<!-- 这两个包分别是Jedis与redisTemplate的使用,其实redisTemplate底层用到了Jedis,后面工具类我会说明两个用法区别-->
<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
</dependency>

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.1.RELEASE</version>
</dependency>​​​​​​​

 

第二步:数据源与配置文件(ApplicationContext-redis.xml  与  redis.properties)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop.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
    ">
    <!-- 加载配置 -->
    <context:property-placeholder location="classpath:redis.properties" />
    
    <!-- jedis连接池配置 -->
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="maxTotal" value="${redis.maxTotal}" /> 
        <property name="blockWhenExhausted" value="true" /> 
    </bean> 

    <!-- jedis连接工程的配置 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="poolConfig" ref="jedisPoolConfig" /> 
        <property name="password" value="" />
        <property name="usePool" value="true"/> 
        <property name="timeout" value="${redis.timeout}"></property>
    </bean>

    <!-- redisTemplate配置 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">   
        <property name="connectionFactory"   ref="jedisConnectionFactory" />  
         
         <!-- 对key的默认序列化器。默认值是StringSerializer -->
        <property name="keySerializer">   
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />   
        </property>      
        <!-- 是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。 -->
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>  
        <!-- 存储Map时key需要的序列化配置 -->
        <property name="hashKeySerializer">     
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>     
        </property>  
        <!-- 存储Map时value需要的序列化配置 --> 
        <property name="hashValueSerializer">   
           <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>     
        </property> 
     </bean>
     
</beans>
#redis中心
#绑定的主机地址
redis.host=127.0.0.1
#指定Redis监听端口,默认端口为6379
redis.port=6379
#授权密码(可以不使用,默认无密码,如果需要,去安装目录下修改配置)
redis.password=******
#最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.maxIdle=100
#最大连接数:能够同时建立的“最大链接个数”
redis.maxTotal=200
#最大等待时间:单位ms
redis.maxWait=1000
#使用连接时,检测连接是否成功 
redis.testOnBorrow=false
#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值