
http://blog.mkfree.com/posts/515835d1975a30cc561dc35d
spring-data-redis API:http://docs.spring.io/spring-data/redis/docs/1.5.1.RELEASE/api/
首先跟大家道歉,為什么呢?在不久之前,寫了一篇http://blog.mkfree.com/posts/12,簡單地使用是沒有問題的,但如果在並發量高的時候,問題就會慢慢出現了,是什么問題呢?
當在高並發的情況下,向redis發出請求,每次操作都新建了一個連接,並且調用了一次連接就不釋放也不重新使用,沒有做連接池,雖然Redis的服務端,有一個機制是當客戶端過一些時間沒有操作時就關閉客戶端連接。
這樣有兩個缺點:
1.在一定時間內,客戶端連接數太多,在關閉連接時,也是非常消耗性能
2.達到一定的連接數時,服務端直接報錯,連接數過多
經測試總結到以上兩個問題.那應該怎么辦呢?
其實,客戶端連接可以放到一個連接池里,用完了,放回到連接池中,這樣可以重復調用。。這樣就可以限制一定數理的連接數,並且性能方面也優化了。。。后來我在官方上看到了spring data redis RedisTemplate,它封裝了redis連接池管理的邏輯,業務代碼無須關心獲取,釋放連接邏輯;spring redis同時支持了Jedis,Jredis,rjc 客戶端操作;
下面就來看看具體的程序代碼:
我首先定義了一些操作接口,我為了方便使用,重新封裝下RedisTemplate.
RedisService 想要更多的操作自己在接口里添加啦
package com.mkfree.framework.common.redis;
import java.util.Set;
/**
* redis 的操作開放接口
*
* @author hk
*
* 2013-3-31 下午7:25:42
*/
public interface RedisService {
/**
* 通過key刪除
*
* @param key
*/
public abstract long del(String... keys);
/**
* 添加key value 並且設置存活時間(byte)
*
* @param key
* @param value
* @param liveTime
*/
public abstract void set(byte[] key, byte[] value, long liveTime);
/**
* 添加key value 並且設置存活時間
*
* @param key
* @param value
* @param liveTime
* 單位秒
*/
public abstract void set(String key, String value, long liveTime);
/**
* 添加key value
*
* @param key
* @param value
*/
public abstract void set(String key, String value);
/**
* 添加key value (字節)(序列化)
*
* @param key
* @param value
*/
public abstract void set(byte[] key, byte[] value);
/**
* 獲取redis value (String)
*
* @param key
* @return
*/
public abstract String get(String key);
/**
* 通過正則匹配keys
*
* @param pattern
* @return
*/
public abstract Setkeys(String pattern);
/**
* 檢查key是否已經存在
*
* @param key
* @return
*/
public abstract boolean exists(String key);
/**
* 清空redis 所有數據
*
* @return
*/
public abstract String flushDB();
/**
* 查看redis里有多少數據
*/
public abstract long dbSize();
/**
* 檢查是否連接成功
*
* @return
*/
public abstract String ping();
}
RedisServiceImpl 接口實現類
package com.mkfree.framework.common.redis;
import java.io.UnsupportedEncodingException;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* 封裝redis 緩存服務器服務接口
*
* @author hk
*
* 2012-12-16 上午3:09:18
*/
@Service(value = "redisService")
public class RedisServiceImpl implements RedisService {
private static String redisCode = "utf-8";
/**
* @param key
*/
public long del(final String... keys) {
return redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (int i = 0; i < keys.length; i++) {
result = connection.del(keys[i].getBytes());
}
return result;
}
});
}
/**
* @param key
* @param value
* @param liveTime
*/
public void set(final byte[] key, final byte[] value, final long liveTime) {
redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.set(key, value);
if (liveTime > 0) {
connection.expire(key, liveTime);
}
return 1L;
}
});
}
/**
* @param key
* @param value
* @param liveTime
*/
public void set(String key, String value, long liveTime) {
this.set(key.getBytes(), value.getBytes(), liveTime);
}
/**
* @param key
* @param value
*/
public void set(String key, String value) {
this.set(key, value, 0L);
}
/**
* @param key
* @param value
*/
public void set(byte[] key, byte[] value) {
this.set(key, value, 0L);
}
/**
* @param key
* @return
*/
public String get(final String key) {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
return new String(connection.get(key.getBytes()), redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
});
}
/**
* @param pattern
* @return
*/
public Setkeys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.execute(new RedisCallback() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(key.getBytes());
}
});
}
/**
* @return
*/
public String flushDB() {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return "ok";
}
});
}
/**
* @return
*/
public long dbSize() {
return redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.dbSize();
}
});
}
/**
* @return
*/
public String ping() {
return redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
return connection.ping();
}
});
}
private RedisServiceImpl() {
}
@Autowired
private RedisTemplate<string, string=""> redisTemplate;
}
spring-context.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 掃描注解Bean -->
<context:component-scan base-package="com.mkfree.framework.common.redis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 初始化屬性文件 -->
<bean id="propertyConfigurer" class="com.mkfree.framework.common.spring.MkfreePropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/project.properties</value>
</list>
</property>
</bean>
<!-- 配置redis 緩存服務器 -->
<bean id="connectionFactory" 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}" /> -->
</bean>
<!-- redis操作模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory">
<ref bean="connectionFactory"/>
</property>
</bean>
</beans>
最后就是簡單的測試用例了 RedisServiceTest
package com.mkfree.framework.common.redis;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RedisServiceTest {
ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
RedisService redisService = (RedisService) app.getBean("redisService");
@Test
public void del() {
redisService.set("a1", "a1");
long result = redisService.del("a1");
Assert.assertEquals(1L, result);
}
@Test
public void set() {
redisService.set("a1", "a1");
}
@Test
public void get() {
redisService.set("a1", "a1");
String result = redisService.get("a1");
Assert.assertEquals("a1", result);
}
}
