redis提供鍵值對的形式對數據進行存儲。支持五種數據類型:String(字符串),List(鏈表),Hash(散列),Set(無序集合),ZSet(有序集合)。下面是網上對其數據結構簡單的概括比較好的,以下:java
結構類型 | 結構存儲的值 | 結構的讀寫能力 |
---|---|---|
String | 能夠是字符串、整數或者浮點數 | 對整個字符串或者字符串的其中一部分執行操做;對象和浮點數執行 自增(increment)或者自減(decrement) |
List | 一個鏈表,鏈表上的每一個節點都包含了一個字符串 | 從鏈表的兩端推入或者彈出元素;根據偏移量對鏈表進行修剪(trim);讀取單個或者多個元素;根據值來查找或者移除元素 |
Hash | 包含鍵值對的無序散列表 | 添加、獲取、移除單個鍵值對;獲取全部鍵值對 |
Set | 包含字符串的無序收集器(unorderedcollection),而且被包含的每一個字符串都是獨一無二的、各不相同 | 添加、獲取、移除單個元素;檢查一個元素是否存在於某個集合中;計算交集、並集、差集;從集合裏賣弄隨機獲取元素 |
ZSet | 字符串成員(member)與浮點數分值(score)之間的有序映射,元素的排列順序由分值的大小決定 | 添加、獲取、刪除單個元素;根據分值範圍(range)或者成員來獲取元素 |
對於JAVA語言,咱們以前使用Jedis對redis進行基本的指令操做,隨着Spring對Jedis進行了很好的封裝以後,使用Spring-data-redis包對redis的操做變得更加簡單和方便。而Spring-data-Redis則是經過RedisTemplate對象來操做Redis的五種數據結構。redis
<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>
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>
推薦使用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數據結構