Redis是當前比較熱門的NOSQL系統之一, 它是一個key-value存儲系統,和memcached相似,但在很大程度上補足了memcached的不足,它支持存儲的value類型相對較多,包括String,List,Set和Hash,這些數據類型都支持push/pop,add/remove操做,在此基礎上,redis支持各類不一樣方式的排序,Redis數據都是緩存在計算機內存中, 而且會週期性的把更新的數據寫入磁盤或者把修改操做寫入追加的記錄文件程序員
redis官網地址: http://redis.io/web
性能極高 – Redis能讀的速度是110000次/s,寫的速度是81000次/s 。redis
豐富的數據類型 – Redis支持二進制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 數據類型操做。spring
原子 – Redis的全部操做都是原子性的,同時Redis還支持對幾個操做全並後的原子性執行。數據庫
豐富的特性 – Redis還支持 publish/subscribe, 通知, key 過時等等特性。centos
Redis有着更爲複雜的數據結構而且提供對他們的原子性操做,這是一個不一樣於其餘數據庫的進化路徑。Redis的數據類型都是基於基本數據結構的同時對程序員透明,無需進行額外的抽象。緩存
Redis運行在內存中可是能夠持久化到磁盤,因此在對不一樣數據集進行高速讀寫時須要權衡內存,應爲數據量不能大於硬件內存。在內存數據庫方面的另外一個優勢是,相比在磁盤上相同的複雜的數據結構,在內存中操做起來很是簡單,這樣Redis能夠作不少內部複雜性很強的事情。同時,在磁盤格式方面他們是緊湊的以追加的方式產生的,由於他們並不須要進行隨機訪問session
下載redis數據結構
$ wget http://download.redis.io/releases/redis-2.8.3.tar.gz $ tar -zxvf redis-2.8.3.tar.gz $ cd redis-2.8.3 $ make
安裝redis可能會出現下面的錯誤併發
#make
cd src && make all
make[1]: Entering directory `/software/redis-2.8.8/src'
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/software/redis-2.8.8/src'
make: *** [all] Error 2
解決方案執行下面的命令
安裝gccyuan
yum -y install gcc*
make MALLOC=libc
緣由是沒有安裝jemalloc
內存分配器,能夠安裝 jemalloc
2. 編譯完成, 進入src目錄下有四個可執行文件redis-server, redis-benchmark, redis-cli和redis.conf
3. 用客戶端測試
jchen19@server1:~$ redis-cli redis 127.0.0.1:6379> set name "test" OK redis 127.0.0.1:6379> get name "test" redis 127.0.0.1:6379> set foo "bar" OK redis 127.0.0.1:6379> get foo "bar" redis 127.0.0.1:6379>
至此redis已經成功安裝
centos安裝redis可經過如下命令完成:
yum install redis
啓動redis
service redis start
1.插入數據 redis 127.0.0.1:6379> set name "test" OK 設置一個key-value對 2.查詢數據 redis 127.0.0.1:6379> get name "test" 取出key所對應的value 3.刪除鍵值 redis 127.0.0.1:6379>del name 刪除這個key及對應value 4.驗證鍵是否存在 redis 127.0.0.1:6379>exists name (integer)0 其中0表明key不存在, 1表明存在
引入依賴
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
web.xml中添加filter
<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
配置redis
@Configuration @EnableCaching @EnableRedisHttpSession public class SpringRedisConfig extends CachingConfigurerSupport { @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(getRedisUrl()); redisConnectionFactory.setPort(Integer.parseInt(getRedisPort())); redisConnectionFactory.setPassword(getRedisPassword()); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setKeySerializer(new StringRedisSerializer()); return redisTemplate; } @Bean public RedisTemplate<String, Long> longRedisTemplate() { final RedisTemplate<String, Long> template = new RedisTemplate<String, Long>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class)); template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class)); return template; } @Bean public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // 啓用前綴後,生成的key會自動加上cache名稱,至關於命名空間。最終的緩存key爲cachename:key的方式。 cacheManager.setUsePrefix(true); ArrayList<String> cacheNames = new ArrayList<String>(); cacheNames.add("campInfo"); cacheNames.add(ConfigService.getAppConfig().getProperty("DUPLICATE_REMOVAL", "")); cacheManager.setCacheNames(cacheNames); Map<String, Long> cacheExp = new HashMap<String, Long>(); cacheExp.put("campInfo", new Long(172800)); cacheExp.put(ConfigService.getAppConfig().getProperty("DUPLICATE_REMOVAL", ""), new Long(60)); cacheManager.setExpires(cacheExp); // // 設置默認失效時長爲7200秒 // cacheManager.setDefaultExpiration(7200); return cacheManager; } protected String getRedisUrl() { return "192.168.102.255"; } protected String getRedisPort() { return "6379"; } protected String getRedisPassword() { return "123"; } }