关于redisTemplate对list泛型的操作

 

redis配置

#--------- redis --------------
redis.master.ip=localhost
redis.master.port=6379
# 自己添加的密码
redis.pass=****

 

spring-redis.xml文件的配置

<?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:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 以前项目中的配置,注意需要添加Spring Data Redis等jar包 -->
    <description>redis配置</description>



    <!--jedis连接池配置-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <property name="maxTotal" value="${redis.pool.maxActive}"/>
        <property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
    </bean>
    
    <!-- JedisConnectionFactory -->
    <!--redis 连接工厂-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.master.ip}"/>
        <property name="port" value="${redis.master.port}"/>
        <property name="poolConfig" ref="jedisPool"/>
        <property name="password" value="${redis.pass}" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="jedisConnectionFactory">
        <property name="keySerializer">
            <bean class="com.wangzhixuan.commons.redis.serialize.RedisKeySerializer"></bean>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="com.wangzhixuan.commons.redis.serialize.RedisKeySerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

    <!--spring cache-->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
          c:redisOperations-ref="redisTemplate">
        <!-- 默认缓存10分钟 -->
        <property name="defaultExpiration" value="600"/>
        <property name="usePrefix" value="true"/>
        <!-- cacheName 缓存超时配置,半小时,一小时,一天 -->
        <property name="expires">
            <map key-type="java.lang.String" value-type="java.lang.Long">
                <entry key="halfHour" value="1800"/>
                <entry key="hour" value="3600"/>
                <entry key="oneDay" value="86400"/>
                <!-- shiro cache keys -->
                <entry key="authorizationCache" value="1800"/>
                <entry key="authenticationCache" value="1800"/>
                <entry key="activeSessionCache" value="1800"/>
            </map>
        </property>
    </bean>

    <!-- 配置redis客户端实现类 -->
     	<bean id="redis" class="redis.clients.jedis.Jedis"/>


    <!-- cache注解,和spring-ehcache.xml中的只能使用一个 -->
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
</beans>

 

Redis key序列化,支持更多基本类型
RedisKeySerializer类
package com.wangzhixuan.commons.redis.serialize;

import java.nio.charset.Charset;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;

import com.wangzhixuan.commons.utils.Charsets;

/**
 * Redis key序列化,支持更多基本类型
 * 
 * @author L.cm
 *
 */
public class RedisKeySerializer implements RedisSerializer<Object> {

	private final Charset charset;
	private final ConversionService converter;

	public RedisKeySerializer() {
		this(Charsets.UTF_8);
	}

	public RedisKeySerializer(Charset charset) {
		Assert.notNull(charset, "Charset must not be null!");
		this.charset = charset;
		this.converter = DefaultConversionService.getSharedInstance();
	}

	@Override
	public Object deserialize(byte[] bytes) {
		throw new RuntimeException("Used only for serializing key, not for deserialization.");
	}

	@Override
	public byte[] serialize(Object object) {
		if (object == null) {
			return null;
		}
		String string = converter.convert(object, String.class);
		return string.getBytes(charset);
	}

}

 

注解:

​
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

​

使用redisTemplate存list<V> 。

  long data1=System.currentTimeMillis();

    HashMap map=new HashMap();
            map.put("page",0);
            map.put("rows",10);
            List<Product> lists=commodityService.getAllProduct(map);
            System.out.println(lists.size());
            long data2=System.currentTimeMillis();
            redisTemplate.opsForList().leftPush("c",lists);
            System.out.println("数据库查询耗时:"+(data2-data1));

返回结果中:耗时:238毫秒,

 

将相同的数据存在redis里:

   ListOperations<String , Object> proList= redisTemplate.opsForList();
           
//            System.out.println("缓存中的数据:"+proList.range("c",0,-1));
            long data2=System.currentTimeMillis();
            System.out.println("缓存耗时:"+(data2-data1));

在redis里的取数据:25毫秒。

 

往redis存一个V类型的我这里用字符串做了演示:

   // 往redis里添加key--value
        redisTemplate.boundValueOps("name2").set("itcast");

 

取一个V类型的:

  Object name=redisTemplate.boundValueOps("name2").get();
        System.out.println(name);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小tu豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值