RedisTemplate访问Redis数据结构(前言)

Redis五种基本数据结构

redis提供键值对的形式对数据进行存储。支持五种数据类型:String(字符串),List(链表),Hash(散列),Set(无序集合),ZSet(有序集合)。下面是网上对其数据结构简单的概括比较好的,以下:java

 

结构类型 结构存储的值 结构的读写能力
String  能够是字符串、整数或者浮点数 对整个字符串或者字符串的其中一部分执行操做;对象和浮点数执行 自增(increment)或者自减(decrement)
List  一个链表,链表上的每一个节点都包含了一个字符串 从链表的两端推入或者弹出元素;根据偏移量对链表进行修剪(trim);读取单个或者多个元素;根据值来查找或者移除元素
Hash  包含键值对的无序散列表 添加、获取、移除单个键值对;获取全部键值对
Set 包含字符串的无序收集器(unorderedcollection),而且被包含的每一个字符串都是独一无二的、各不相同 添加、获取、移除单个元素;检查一个元素是否存在于某个集合中;计算交集、并集、差集;从集合里卖弄随机获取元素
ZSet 字符串成员(member)与浮点数分值(score)之间的有序映射,元素的排列顺序由分值的大小决定 添加、获取、删除单个元素;根据分值范围(range)或者成员来获取元素


 

 

 

 

 

Spring-data-Redis简介

对于JAVA语言,咱们以前使用Jedis对redis进行基本的指令操做,随着Spring对Jedis进行了很好的封装以后,使用Spring-data-redis包对redis的操做变得更加简单和方便。而Spring-data-Redis则是经过RedisTemplate对象来操做Redis的五种数据结构。redis

如何引入Spring-data-Redis

1.导入jar包:spring-data-redis-1.8.7.RELEASE.jar 和 jedis-2.9.0.jar

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

 

2.配置文件(SpringBoot方式暂不介绍)

redis.propertiesspring

redis.host=192.168.132.128
redis.port=10000
redis.password=123456

redis.minIdle=50
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

applicationContext.xmljson

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <!-- 引入小配置文件-->
            <value>classpath:redis.properties</value>
        </list>
    </property>
</bean>

<!-- 链接池 ,本质是对GenericObjectPoolConfig的属性的设置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
    <property name="minIdle" value="${redis.minIdle}" />  
    <property name="maxIdle" value="${redis.maxIdle}" />    
    <property name="maxTotal" value="${redis.maxActive}" />    
    <property name="maxWaitMillis" value="${redis.maxWait}" />    
    <property name="testOnBorrow" value="${redis.testOnBorrow}" />    
</bean>    

<!-- REDIS链接工厂 -->
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
    <!-- 基础链接参数 -->
    <property name="hostName" value="${redis.host}" />  
    <property name="port" value="${redis.port}" />  
    <property name="password" value="${redis.password}" />  
    <!-- 是否启用链接池 -->
    <property name="usePool" value="true" />  
    <property name="poolConfig" ref="poolConfig" />  
</bean>   

<!-- 对String类型处理的RedisTemplate -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
    <property name="connectionFactory" ref="jedisConnFactory" />  
    <property name="keySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="valueSerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="hashKeySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="hashValueSerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property> 
</bean>

 <!-- 对LIST,SET,ZSET,HASH等类型的处理RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    <property name="connectionFactory" ref="jedisConnFactory"/>  
    <property name="keySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    </property>
    <property name="hashKeySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    </property>
    <property name="valueSerializer">
        <!-- 对象序列化方案 -->
        <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    </property>
    <property name="hashValueSerializer">
        <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
    </property>
</bean>

 

3.注意

推荐使用GenericJackson2JsonRedisSerializer,而不是Jackson2JsonRedisSerializer,由于GenericJackson2JsonRedisSerializer提供了很好的对泛型的支持,而使用Jackson2JsonRedisSerializer对不一样对象进行操做时都须要手动set序列化方案,不能直接集成到配置文件中将其直接托管给spring工厂。固然,咱们能够自定义序列化方案,同时也可使用spring-data-redis集成好的序列化方案,例如集成号称速度最快的fastjson序列化方案,下面提供一个fastjson的Serializer(暂时没有集成对泛型归入工厂方案的支持)。springboot

package util;

import java.nio.charset.Charset;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * FASTJSON序列化工具
 * @author LiuChengxiang
 * @time 2017年9月19日上午9:30:27
 * 
 * @param <T>
 */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private Class<T> clazz;

    public FastJson2JsonRedisSerializer(Class<T> clazz){
        super();
        this.clazz = clazz;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        return (T)JSON.parseObject(str,clazz);
    }

}

 转载自:https://blog.csdn.net/weixin_37490221/article/details/78134105数据结构

相关文章
相关标签/搜索