关于redisTemplate对list泛型的操作

 

redis配置

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

 

spring-redis.xml文件的配置




    
    redis配置



    
    
        
        
        
        
        
    
    
    
    
    
        
        
        
        
    

    
        
            
        
        
            
        
        
            
        
        
            
        
    

    
    
        
        
        
        
        
            
                
                
                
                
                
                
                
            
        
    

    
     	


    
    

 

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 {

	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 redisTemplate;

​

使用redisTemplate存list

  long data1=System.currentTimeMillis();

    HashMap map=new HashMap();
            map.put("page",0);
            map.put("rows",10);
            List 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 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);

 

你可能感兴趣的:(关于redisTemplate对list泛型的操作)