java web項目配置Redis
Redis介紹
REmote DIctionary Server(Redis) 是一個由Salvatore Sanfilippo寫的key-value存儲系統。Redis是一個開源的使用ANSI C語言編寫、遵守BSD協議、支持網絡、可基於內存亦可持久化的日志型、Key-Value數據庫,並提供多種語言的API。它通常被稱為數據結構服務器,因為值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等類型。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數據都是緩存在內存中。
安裝redis
環境准備:win 7環境、reids3.0
步驟1:下載redis解壓到任意盤符,結果如下:

步驟2:點擊redis-server.exe啟動redis服務;
步驟3:選擇redis-cli.exe啟動redis客戶端,進行測試。輸入set mykey redis 回車,輸入 get mykey 回車,輸出:“redis”。安裝完成.
項目配置reids
spring項目下,需要導入commons-pool-1.5.6.jar、commons-pool2-2.4.2.jar、jedis-2.7.3.jar、spring-data-redis-1.6.0.RELEASE.jar文件。在spring配置文件中加入以下元素:
<bean id="redisUtil" class="com.project.controller.Redis.RedisUtil">
<!-- 初始化類 -->
<property name="addr"><value>127.0.0.1</value></property>
<!-- 訪問地址,默認本地 -->
<property name="port"><value>6379</value></property>
<!-- 端口號 -->
<property name="auth"><value>master</value></property>
<property name="maxIdle"><value>200</value></property>
<property name="maxActive"><value>1024</value></property>
<property name="maxWait"><value>10000</value></property>
<property name="timeOut"><value>10000</value></property>
<property name="testOnBorrow"><value>true</value></property>
</bean>
RedisUtil代碼:
public class RedisUtil implements Serializable{
private static final long serialVersionUID = -1149678082569464779L;
//Redis服務器IP
private static String addr;
//Redis的端口號
private static int port;
//訪問密碼
private static String auth;
//可用連接實例的最大數目,默認值為8;
//如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
private static int maxActive;
//控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例,默認值也是8。
private static int maxIdle;
//等待可用連接的最大時間,單位毫秒,默認值為-1,表示永不超時。如果超過等待時間,則直接拋出JedisConnectionException;
private static int maxWait;
private static int timeOut;
//在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的;
private static boolean testOnBorrow;
public static Jedis jedis;//非切片額客戶端連接
public static JedisPool jedisPool;//非切片連接池
public static ShardedJedis shardedJedis;//切片額客戶端連接
public static ShardedJedisPool shardedJedisPool;//切片連接池
private static void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
jedisPool = new JedisPool(config, addr, port);
}
private static void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
// slave鏈接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(addr, port, auth));
// 構造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public static void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
initialPool();
initialShardedPool();
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
System.out.println("連接shardedJedisPool失敗!");
}
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
System.out.println("連接jedisPool失敗!");
}
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public int getTimeOut() {
return timeOut;
}
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
功能測試
//redis-server.exe啟動狀態下 public void selectReviewsByGoodsId(HttpServletRequest request){ try { Jedis jedis = new Jedis(); Date time1 = new Date(); jedis.set("name","zhang"); Date time2 = new Date(); System.out.println("時間:"+(time2.getTime()-time1.getTime())); System.out.println("存入redis完畢"); System.out.println(jedis.get("name")); } catch (Exception e) { //如果緩存連不上,則不處理 System.out.println("登錄無法更新該用戶緩存"); } }
這是剛學redis和項目配置的時候學的;希望可以共同進步
