注意:本章代碼將會建立在上一章的代碼基礎上,上一章鏈接《第八章 企業項目開發--分布式緩存memcached》
1、為什么用Redis
1.1、為什么用分布式緩存(或者說本地緩存存在的問題)?
1.2、有了memcached,為什么還要用redis?
- 見《第一章 常用的緩存技術》
2、代碼實現
2.1、ssmm0

pom.xml
只在dev環境下添加了以下代碼:
<!--
redis:多台服務器支架用什么符號隔開無所謂,只要在程序中用相應的符號去分隔就好。
這里只配置了一個redis.servers,如果系統特別大的時候,可以為每一種業務或某幾種業務配置一個redis.xxx.servers
-->
<redis.servers><![CDATA[127.0.0.1:6379]]></redis.servers>
<!--
下邊各個參數的含義在RedisFactory.java中有介紹,
當我們三種環境(dev/rc/prod)下的一些參數都相同時,可以將這些參數直接設置到cache_conf.properties文件中去
-->
<redis.timeout>2000</redis.timeout><!-- 操作超時時間:2s,單位:ms -->
<redis.conf.lifo>true</redis.conf.lifo>
<redis.conf.maxTotal>64</redis.conf.maxTotal>
<redis.conf.blockWhenExhausted>true</redis.conf.blockWhenExhausted>
<redis.conf.maxWaitMillis>-1</redis.conf.maxWaitMillis>
<redis.conf.testOnBorrow>false</redis.conf.testOnBorrow>
<redis.conf.testOnReturn>false</redis.conf.testOnReturn>
<!-- 空閑連接相關 -->
<redis.conf.maxIdle>8</redis.conf.maxIdle>
<redis.conf.minIdle>0</redis.conf.minIdle>
<redis.conf.testWhileIdle>true</redis.conf.testWhileIdle>
<redis.conf.timeBetweenEvictionRunsMillis>30000</redis.conf.timeBetweenEvictionRunsMillis><!-- 30s -->
<redis.conf.numTestsPerEvictionRun>8</redis.conf.numTestsPerEvictionRun>
<redis.conf.minEvictableIdleTimeMillis>60000</redis.conf.minEvictableIdleTimeMillis><!-- 60s -->
注意:看注釋。
完整版的根pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>ssmm0</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ssmm0</name>
<packaging>pom</packaging><!-- 父模塊 -->
<!-- 管理子模塊 -->
<modules>
<module>userManagement</module><!-- 具體業務1-人員管理系統 -->
<module>data</module><!-- 封裝數據操作 -->
<module>cache</module><!-- 緩存模塊 -->
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- dependencyManagement不會引入實際的依賴,只是作為一個依賴池,供其和其子類使用 -->
<dependencyManagement>
<dependencies>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.39</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<!-- 這個是使用velocity的必備包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
<scope>runtime</scope>
</dependency>
<!-- 數據源 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.47</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<!-- velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>velocity-tools</groupId>
<artifactId>velocity-tools-generic</artifactId>
<version>1.2</version>
</dependency>
<!-- 用於加解密 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<!-- 集合工具類 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<!-- 字符串處理類 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.6</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 引入實際依賴 -->
<dependencies>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 集合工具類 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<!-- 字符串處理類 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<!-- 這里配置了這一塊兒true,才可以讓指定文件(這里是src/main/resources/spring-data.xml)讀到pom.xml中的配置信息
, 值得注意的是,如果src/main/resources下還有其他文件,而你不想讓其讀pom.xml, 你還必須得把src/main/resources下的其余文件再配置一遍,配置為false(不可讀pom.xml),
如下邊的注釋那樣,否則,會報這些文件找不到的錯誤
-->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
<!--
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>*.properties</include>
</includes>
</resource>
-->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<!-- 這里如果不加這一條,那么在spring-data.xml中配置的xml將找不到classpath:mapper/admin/AdminMapper.xml -->
<include>mapper/**/*.xml</include>
</includes>
</resource>
</resources>
</build>
<!--
profiles可以定義多個profile,然后每個profile對應不同的激活條件和配置信息,從而達到不同環境使用不同配置信息的效果
注意兩點:
1)<activeByDefault>true</activeByDefault>這種情況表示服務器啟動的時候就采用這一套env(在這里,就是prod)
2)當我們啟動服務器后,想采用開發模式,需切換maven的env為dev,如果env的配置本身就是dev,需要將env換成rc或prod,點擊apply,然后再將env切換成dev,點擊apply才行
-->
<profiles>
<!-- 開發env -->
<profile>
<id>dev</id>
<activation>
<!-- 這里為了測試方便,改為了true,在上線的時候一定要改成false,否則線上使用的就是這一套dev的環境了 -->
<activeByDefault>true</activeByDefault>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<env>dev</env>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<!--
對於jdbc.url中內容的配置,如果需要配置 &時,有兩種方法:
1)如下邊這樣,使用<![CDATA[XXX]]>包起來
2)使用jdbc.properties文件來讀取此pom.xml,然后spring.xml再讀取jdbc.properties文件 顯然,前者更方便,而且還省了一個jdbc.properties的文件,但是,有的時候,還是會用后者的;
在使用后者的時候,注意三點:
1)需要修改上邊的build中的內容
2)需要在spring.xml中配置<context:property-placeholder location="classpath:jdbc.properties"/>
3)將jdbc.properties放在ssmm0-data項目中,之后需要將ssmm0-data項目的env配置為dev
-->
<jdbc.url><![CDATA[jdbc:mysql://127.0.0.1:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>123456</jdbc.password>
<!-- memcache,多台服務器之間需要使用空格隔開,而不要使用英文逗號隔開,因為Xmemcached的AddrUtil源碼是根據空格隔開的 -->
<memcached.servers><![CDATA[127.0.0.1:11211]]></memcached.servers>
<memcached.max.client>10</memcached.max.client><!-- 最多的客戶端數 -->
<memcached.expiretime>900</memcached.expiretime><!-- 過期時間900s -->
<memcached.hash.consistent>true</memcached.hash.consistent><!-- 是否使用一致性hash算法 -->
<memcached.connection.poolsize>1</memcached.connection.poolsize><!-- 每個客戶端池子的連接數 -->
<memcached.op.timeout>2000</memcached.op.timeout><!-- 操作超時時間 -->
<!--
redis:多台服務器支架用什么符號隔開無所謂,只要在程序中用相應的符號去分隔就好。
這里只配置了一個redis.servers,如果系統特別大的時候,可以為每一種業務或某幾種業務配置一個redis.xxx.servers
-->
<redis.servers><![CDATA[127.0.0.1:6379]]></redis.servers>
<!--
下邊各個參數的含義在RedisFactory.java中有介紹,
當我們三種環境(dev/rc/prod)下的一些參數都相同時,可以將這些參數直接設置到cache_conf.properties文件中去
-->
<redis.timeout>2000</redis.timeout><!-- 操作超時時間:2s,單位:ms -->
<redis.conf.lifo>true</redis.conf.lifo>
<redis.conf.maxTotal>64</redis.conf.maxTotal>
<redis.conf.blockWhenExhausted>true</redis.conf.blockWhenExhausted>
<redis.conf.maxWaitMillis>-1</redis.conf.maxWaitMillis>
<redis.conf.testOnBorrow>false</redis.conf.testOnBorrow>
<redis.conf.testOnReturn>false</redis.conf.testOnReturn>
<!-- 空閑連接相關 -->
<redis.conf.maxIdle>8</redis.conf.maxIdle>
<redis.conf.minIdle>0</redis.conf.minIdle>
<redis.conf.testWhileIdle>true</redis.conf.testWhileIdle>
<redis.conf.timeBetweenEvictionRunsMillis>30000</redis.conf.timeBetweenEvictionRunsMillis><!-- 30s -->
<redis.conf.numTestsPerEvictionRun>8</redis.conf.numTestsPerEvictionRun>
<redis.conf.minEvictableIdleTimeMillis>60000</redis.conf.minEvictableIdleTimeMillis><!-- 60s -->
</properties>
</profile>
<!-- 預上線env -->
<profile>
<id>rc</id>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>env</name>
<value>rc</value>
</property>
</activation>
<properties>
<env>rc</env>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<!-- 假設的一個地址 -->
<jdbc.url><![CDATA[jdbc:mysql://10.10.10.100:3306/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url>
<jdbc.username>root2</jdbc.username>
<jdbc.password>1234562</jdbc.password>
</properties>
</profile>
<!-- 線上env -->
<profile>
<id>prod</id>
<activation>
<!-- 這里為了測試方便,改為了false,在上線的時候一定要改成true,否則線上使用的就不是這一套環境了 -->
<activeByDefault>false</activeByDefault>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
<properties>
<env>prod</env>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<!-- 假設的一個地址 -->
<jdbc.url><![CDATA[jdbc:mysql://99.99.99.999:3307/blog?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8]]></jdbc.url>
<jdbc.username>sadhijhqwui</jdbc.username>
<jdbc.password>zxczkchwihcznk=</jdbc.password>
</properties>
</profile>
</profiles>
</project>
2.2、ssmm0-cache

pom.xml完整版(只是添加了jedis的依賴包)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 指定父模塊 -->
<parent>
<groupId>com.xxx</groupId>
<artifactId>ssmm0</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xxx.ssmm0</groupId>
<artifactId>ssmm0-cache</artifactId>
<name>ssmm0-cache</name>
<packaging>jar</packaging>
<!-- 引入實際依賴 -->
<dependencies>
<!-- memcached -->
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>1.4.3</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
</project>
cache_config.properties(添加了redis相關配置)
#memcached配置#
#memcached服務器集群
memcached.servers = ${memcached.servers}
#緩存過期時間
memcached.expiretime = ${memcached.expiretime}
#是否使用一致性hash算法
memcached.hash.consistent = ${memcached.hash.consistent}
#memcached的最大客戶端數量
memcached.max.client = ${memcached.max.client}
#每個客戶端池子的連接數
memcached.connection.poolsize = ${memcached.connection.poolsize}
#操作超時時間
memcached.op.timeout = ${memcached.op.timeout}
#redis配置#
#redis集群
redis.servers = ${redis.servers}
#超時時間
redis.timeout = ${redis.timeout}
#是否啟用后進先出
redis.conf.lifo = ${redis.conf.lifo}
#最多創建幾個ShardJedis,即連接
redis.conf.maxTotal = ${redis.conf.maxTotal}
#連接耗盡是否阻塞等待
redis.conf.blockWhenExhausted = ${redis.conf.blockWhenExhausted}
#等待獲取連接的最長時間
redis.conf.maxWaitMillis = ${redis.conf.maxWaitMillis}
#獲取連接前,是否對連接進行測試
redis.conf.testOnBorrow = ${redis.conf.testOnBorrow}
#歸還連接前,是否對連接進行測試
redis.conf.testOnReturn = ${redis.conf.testOnReturn}
#最大空閑連接數
redis.conf.maxIdle = ${redis.conf.maxIdle}
#最小空閑連接數
redis.conf.minIdle = ${redis.conf.minIdle}
#對空閑連接進行掃描,檢查連接有效性
redis.conf.testWhileIdle = ${redis.conf.testWhileIdle}
#兩次掃描空閑連接的時間間隔
redis.conf.timeBetweenEvictionRunsMillis = ${redis.conf.timeBetweenEvictionRunsMillis}
#每次空閑掃描時掃描的控線連接的個數
redis.conf.numTestsPerEvictionRun = ${redis.conf.numTestsPerEvictionRun}
#一個空閑連接至少連續保持多長時間空閑才會被空閑掃描
redis.conf.minEvictableIdleTimeMillis = ${redis.conf.minEvictableIdleTimeMillis}
7個Java類:
- RedisFactory:構建ShardJedisPool。
- 一個ShardJedisPool中配置了多個JedisShardInfo
- 每一個JedisShardInfo都是一個server
- 一個ShardJedisPool中可以獲取多個ShardJedis連接實例,具體數目由maxTotal屬性而定
- RedisBaseUtil:獲取獲取ShardJedis連接與歸還ShardJedis連接(這是其他所有緩存操作都需要的方法)
- RedisStringUtil:redis的第一種數據結構--字符串,操作類
- RedisListUtil:redis的第二種數據結構--list,操作類
- RedisSetUtil:redis的第三種數據結構--set,操作類
- RedisSortedSetUtil:redis的第四種數據結構--sorted set,操作類
- RedisHashUtil:redis的第五種數據結構--hash,操作類
RedisFactory:
1 package com.xxx.cache.redis;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Properties;
6
7 import org.apache.commons.lang3.math.NumberUtils;
8
9 import com.xxx.cache.util.FileUtil;
10
11 import redis.clients.jedis.JedisPoolConfig;
12 import redis.clients.jedis.JedisShardInfo;
13 import redis.clients.jedis.ShardedJedisPool;
14
15 public class RedisFactory {
16 private static ShardedJedisPool jedisPool = null;
17 /**
18 * 構建ShardJedisPool
19 * 一個ShardJedisPool中配置了多個JedisShardInfo
20 * 每一個JedisShardInfo都是一個server
21 * 一個ShardJedisPool中可以獲取多個ShardJedis連接實例,具體數目由maxTotal屬性而定
22 * 注意:
23 * 1、這里只有一個ShardJedisPool,如果你有很多業務,而且不想這些業務都共用幾台redis服務器的話,
24 * 你可以創建多個ShardJedisPool,每個pool中放置不同的服務器即可
25 * 2、這時候多個ShardJedisPool可以放置在一個hashmap中,key由自己指定(寫在一個Enum類中去),key的名稱一般與業務掛鈎就好
26 */
27 static{
28 Properties props = FileUtil.loadProps("cache_config.properties");//加載屬性文件
29 /*
30 * 從屬性文件讀取參數
31 */
32 String servers = props.getProperty("redis.servers", "127.0.0.1:6379");
33 String[] serverArray = servers.split(" ");//獲取服務器數組
34
35 int timeout = FileUtil.getInt(props, "redis.timeout", 5000);//默認:2000ms(超時時間:單位ms)
36 boolean lifo = FileUtil.getBoolean(props, "redis.conf.lifo", true);//默認:true
37
38 int maxTotal = FileUtil.getInt(props, "redis.conf.maxTotal", 64);//默認:8個(最多創建幾個ShardJedis,即連接)
39 boolean blockWhenExhausted = FileUtil.getBoolean(props, "redis.conf.blockWhenExhausted", true);//默認:true(連接耗盡是否阻塞等待)
40 long maxWaitMillis = FileUtil.getLong(props, "redis.conf.maxWaitMillis", -1);//默認:-1,即無限等待(等待獲取連接的最長時間)
41
42 boolean testOnBorrow = FileUtil.getBoolean(props, "redis.conf.testOnBorrow", false);//默認:false(獲取連接前,是否對連接進行測試)
43 boolean testOnReturn = FileUtil.getBoolean(props, "redis.conf.testOnReturn", false);//默認:false(歸還連接前,是否對連接進行測試)
44
45 int maxIdle = FileUtil.getInt(props, "redis.conf.maxIdle", 8);//默認:8(最大空閑連接數)
46 int minIdle = FileUtil.getInt(props, "redis.conf.minIdle", 0);//默認:0(最小空閑連接數)
47 boolean testWhileIdle = FileUtil.getBoolean(props, "redis.conf.testWhileIdle", true);//默認:false(對空閑連接進行掃描,檢查連接有效性)
48 long timeBetweenEvictionRunsMillis = FileUtil.getLong(props, "redis.conf.timeBetweenEvictionRunsMillis", 30000);//默認:-1,(兩次掃描空閑連接的時間間隔)
49 int numTestsPerEvictionRun = FileUtil.getInt(props, "redis.conf.numTestsPerEvictionRun", 3);//默認:3(每次空閑掃描時掃描的控線連接的個數)
50 long minEvictableIdleTimeMillis = FileUtil.getLong(props, "redis.conf.minEvictableIdleTimeMillis", 60000);//默認:30min(一個空閑連接至少連續保持30min中空閑才會被空閑掃描)
51 /*
52 * 配置redis參數
53 */
54 JedisPoolConfig config = new JedisPoolConfig();
55 config.setLifo(lifo);//(last in, first out)是否啟用后進先出,默認true
56 /*
57 * 即原來的maxActive,能夠同時建立的最大連接個數(就是最多分配多少個ShardJedis實例),
58 * 默認8個,若設置為-1,表示為不限制,
59 * 如果pool中已經分配了maxActive個jedis實例,則此時pool的狀態就成exhausted了
60 *
61 * 這里最多可以生產64個shardJedis實例
62 */
63 config.setMaxTotal(maxTotal);
64 config.setBlockWhenExhausted(blockWhenExhausted);//連接耗盡時是否阻塞, false報異常,true阻塞直到超時, 默認true, 達到maxWait時拋出JedisConnectionException
65 config.setMaxWaitMillis(maxWaitMillis);//獲取連接時的最大等待毫秒數(如果設置為阻塞時BlockWhenExhausted),如果超時就拋異常, 小於零:阻塞不確定的時間, 默認-1
66
67 config.setTestOnBorrow(testOnBorrow);//使用連接時,先檢測連接是否成功,若為true,則獲取到的shardJedis連接都是可用的,默認false
68 config.setTestOnReturn(testOnReturn);//歸還連接時,檢測連接是否成功
69
70 /*
71 * 空閑狀態
72 */
73 config.setMaxIdle(maxIdle);//空閑連接數(即狀態為idle的ShardJedis實例)大於maxIdle時,將進行回收,默認8個
74 config.setMinIdle(minIdle);//空閑連接數小於minIdle時,創建新的連接,默認0
75 /*
76 * 在空閑時檢查有效性, 默認false,如果為true,表示有一個idle object evitor線程對idle object進行掃描,
77 * 如果validate失敗,此object會被從pool中drop掉;這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義
78 */
79 config.setTestWhileIdle(testWhileIdle);
80 config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);//表示idle object evitor兩次掃描之間要sleep的毫秒數;
81 config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);//表示idle object evitor每次掃描的最多的對象數;
82 //表示一個對象至少停留在idle狀態的最短時間,然后才能被idle object evitor掃描並驅逐;這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義;
83 config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
84
85 /*
86 * 構建JedisShardInfo集合
87 */
88 List<JedisShardInfo> jedisList = new ArrayList<JedisShardInfo>(1);//我這里只有一台機器,所以傳入參數1,否則默認為10,浪費空間
89 for(String server : serverArray){
90 String[] hostAndPort = server.split(":");
91 /*
92 * 這句代碼中我沒有判斷hostAndPort是不是長度為2,而且端口如果沒有指定或指定錯誤的話,就直接轉到6379
93 * 實際中,我們在配置服務器的時候就一定要注意配置格式正確:host:port
94 */
95 JedisShardInfo shardInfo = new JedisShardInfo(hostAndPort[0],
96 NumberUtils.toInt(hostAndPort[1], 6379),
97 timeout);
98 jedisList.add(shardInfo);
99 }
100 /*
101 * 創建ShardJedisPool
102 */
103 jedisPool = new ShardedJedisPool(config, jedisList);//構建jedis池
104 }
105
106 /**
107 * 如果有多個ShardJedisPool,則需要寫一個hash算法從hashmap中選一個pool返回
108 */
109 public static ShardedJedisPool getJedisPool() {
110 return jedisPool;
111 }
112 }
注意:
- 這里只有一個ShardJedisPool,如果你有很多業務,而且不想這些業務都共用幾台redis服務器的話,你可以創建多個ShardJedisPool,每個pool中放置不同的服務器即可;這時候多個ShardJedisPool可以放置在一個hashmap中,key由自己指定(寫在一個Enum類中去),key的名稱一般與業務掛鈎就好。如果沒說清,看下圖:(在我當前的程序中由於只有一個業務簡單,值設置了一個ShardJedisPool,所以沒有下圖的hashmap)

- 配置參數較多(這里列出幾乎所有的配置參數)
- testOnBorrow:使用連接時,先檢測連接是否成功,若為true,則獲取到的shardJedis連接都是可用的,默認false;在實際使用中,直接使用默認值,因為雖然該參數配置為true可以保證獲取到的連接一定可用,但是由於每次獲取連接都要進行測試,所以效率會變低;考慮到獲取到的連接不可用的概率很低,綜合考慮下,將該值設為false還是比較合適的。
- testOnReturn:同上,在我們下面的程序中可以看到每個緩存操作方法的流程都是"獲取連接-->進行緩存操作-->歸還連接"
- 注意這里jedis的版本是2.6.1,一些配置屬性可能在其他版本看不到(eg.maxTotal),而其他版本的一些屬性可能在該版本中沒有(eg.maxActive)。
- jedis參數介紹參考:http://blog.csdn.net/huahuagongzi99999/article/details/13631579,但是由於版本不同,請注意上邊這一條所說的。
RedisBaseUtil:
1 package com.xxx.cache.redis;
2
3 import redis.clients.jedis.ShardedJedis;
4 import redis.clients.jedis.ShardedJedisPool;
5
6 /**
7 * 獲取ShardJedis與歸還實例
8 */
9 public class RedisBaseUtil {
10 /**
11 * 從ShardJedisPool中獲取ShardJedis
12 */
13 public static ShardedJedis getJedis(){
14 ShardedJedisPool jedisPool = RedisFactory.getJedisPool();//獲取連接池
15 if(jedisPool == null){
16 return null;
17 }
18 return jedisPool.getResource();
19 }
20
21 /**
22 * 歸還jedis實例到連接池中
23 */
24 public static void returnJedis(ShardedJedis jedis, boolean broken){
25 if(jedis==null){//如果傳入的jedis是null的話,不需要歸還
26 return;
27 }
28 ShardedJedisPool jedisPool = RedisFactory.getJedisPool();//獲取連接池
29 if(jedisPool == null){//如果連接池為null的話,不需要歸還
30 return;
31 }
32 if(broken){//如果為true的話,表示是因為發生了異常才歸還
33 jedisPool.returnBrokenResource(jedis);
34 return;
35 }
36 jedisPool.returnResource(jedis);//緩存正常操作結束之后,歸還jedis
37 }
38 }
注意:
- returnBrokenResource:操作被打斷,即沒有正常結束緩存操作,連接歸還
- returnResource:緩存正常操作結束后,連接歸還
RedisStringUtil:
package com.xxx.cache.redis;
import com.xxx.cache.util.CachePrefix;
import redis.clients.jedis.ShardedJedis;
/**
* 字符串緩存操作類或者JavaBean緩存操作類
* key String, value String-->看下邊的注意點2
* key byte[], value byte[]-->key.getBytes[], value 序列化為byte[],通常需要自己寫一個序列化工具
* 注意:這一點與memcached不一樣,memcached可以key String, value Object
* 1、memcached直接加序列化器就可以,或者在業務層中將Object-->String
* 2、redis執行此接口,一般只會采用后者Object-->String
*/
public class RedisStringUtil extends RedisBaseUtil{
private static final String KEY_SPLIT = "-";//用於隔開緩存前綴與緩存鍵值
/**
* 設置緩存
* 類似於memcached的set,不管是否已經有相同的key,都成功
* 實際上只是set(String, String)
*/
public static void set(CachePrefix keyPrefix, String key, String value){
boolean broken = false;//標記:該操作是否被異常打斷而沒有正常結束
ShardedJedis jedis = null;
try {
jedis = getJedis();//獲取jedis實例
if(jedis==null){
broken = true;
return;
}
jedis.set(keyPrefix+KEY_SPLIT+key, value);//set(String,String),value除了string以外,還可以是byte[]
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
}
/**
* 設置緩存,並指定緩存過期時間,單位是秒
*/
public static void setex(CachePrefix keyPrefix, String key, String value, int expire){
boolean broken = false;//該操作是否被異常打斷而沒有正常結束
ShardedJedis jedis = null;
try {
jedis = getJedis();//獲取jedis實例
if(jedis==null){
broken = true;
return;
}
jedis.setex(keyPrefix+KEY_SPLIT+key, expire, value);
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
}
/**
* 設置緩存,如果設置的key不存在,直接設置,如果key已經存在了,則什么操作都不做,直接返回
* 類似於memcached的add
*/
public static boolean setnx(CachePrefix keyPrefix, String key, String value){
boolean broken = false;//該操作是否被異常打斷而沒有正常結束
ShardedJedis jedis = null;
try {
jedis = getJedis();//獲取jedis實例
if(jedis==null){
broken = true;
return false;
}
long setCount = jedis.setnx(keyPrefix+KEY_SPLIT+key, value);
if(setCount == 1){
return true;
}
return false;
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
return false;
}
/**
* 根據key獲取緩存
* @param key
* @return String
*/
public static String get(CachePrefix keyPrefix, String key){
boolean broken = false;//該操作是否被異常打斷而沒有正常結束
ShardedJedis jedis = null;
try {
jedis = getJedis();//獲取jedis實例
if(jedis==null){
broken = true;
return null;
}
return jedis.get(keyPrefix+KEY_SPLIT+key);
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
return null;
}
/**
* 刪除緩存
*/
public static void delete(CachePrefix keyPrefix, String key){
boolean broken = false;//該操作是否被異常打斷而沒有正常結束
ShardedJedis jedis = null;
try {
jedis = getJedis();//獲取jedis實例
if(jedis==null){
broken = true;
return;
}
jedis.del(keyPrefix+KEY_SPLIT+key);
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
}
/**
* 更新緩存過期時間,單位:秒
* 從運行該方法開始,為相應的key-value設置緩存過期時間expire
* 類似於memcached中的touch命令
*/
public static void setExpire(CachePrefix keyPrefix, String key, int expire){
boolean broken = false;
ShardedJedis jedis = null;
try {
jedis = getJedis();
if(jedis==null){
broken = true;
return;
}
jedis.expire(keyPrefix+KEY_SPLIT+key, expire);
} catch (Exception e) {
broken = true;
}finally{
returnJedis(jedis, broken);
}
}
/**
* 測試
*/
public static void main(String[] args) {
//System.out.println(RedisStringUtil.get("hello"));
//RedisStringUtil.delete("hello");
//RedisStringUtil.setex("hello1", "word1", 1);
//RedisStringUtil.setExpire("hello1", 20);
//System.out.println(RedisStringUtil.get("hello1"));
}
}
注意:
- 只有set(string,string)和set(byte[],byte[]),前者類似於Xmemcached的文本協議,后者類似於Xmemcached的二進制協議
- set-->Xmemcached的set,redis上是否已經有與將要存放的key相同的key,都會操作成功
- setnx-->Xmemcached的add,redis上有與將要存放的key相同的key,操作失敗
- expire-->Xmemcached的touch,該方法會在方法執行的時候,為還存在的key-value重新指定緩存過期時間
附:
這里需要安裝一個redis服務器,redis在實際使用中是安裝在Linux上的,我們為了方便,使用windows版本的(我這里使用了redis2.6-win32),如果是64bit的,可以使用redis2.8。
redis2.6(32bit)的文件下載鏈接:http://pan.baidu.com/s/1hri1erq
安裝方式如下:
下載后解壓,此時如果直接雙擊"redis-server.exe",可能會報內存警告,所以先修改redis.conf文件,添加如下配置,如果你下載的上邊的鏈接,可能已經配置了。

之后,以管理員身份運行cmd.exe,並在命令窗口中進入redis-server.exe所在目錄下,執行"redis-server.exe redis.conf"即可。
2.3、ssmm0-data

AdminService:
/*********************redis********************/
public Admin findAdminByIdFromRedis(int id) {
//從緩存中獲取數據
String adminStr = RedisStringUtil.get(CachePrefix.USER_MANAGEMENT, String.valueOf(id));
//若緩存中有,直接返回
if(StringUtils.isNoneBlank(adminStr)){
return Admin.parseJsonToAdmin(adminStr);
}
//若緩存中沒有,從數據庫查詢
Admin admin = adminDao.getUserById(id);
//若查詢出的數據不為null
if(admin!=null){
//將數據存入緩存
RedisStringUtil.set(CachePrefix.USER_MANAGEMENT, String.valueOf(id), admin.toJson());
}
//返回從數據庫查詢的admin(當然也可能數據庫中也沒有,就是null)
return admin;
}
說明:只添加了如上方法,相當於將上一節的memcached緩存換成了redis
2.4、ssmm0-userManagement

AdminController:
/*************************redis******************************/
/**
* 根據id查找Admin
*/
@ResponseBody
@RequestMapping("/findAdminByIdFromRedis")
public Admin findAdminByIdFromRedis(@RequestParam(value="id") int id){
return adminService.findAdminByIdFromRedis(id);
}
說明:只添加了如上方法。
3、測試
首先對ssmm0整個項目"clean compile",然后通過瀏覽器訪問,進行整體測試,測試方法與上一章《第八章 企業項目開發--分布式緩存memcached》完全相同
在以上的代碼中,我只寫了redis的String類型數據結構的緩存操作,其余的四種下一篇再說。

