下載redis最新版本 2.4.10 下載地址:http://redis.googlecode.com/files/redis-2.4.10.tar.gz
安裝的詳細過程就不在這寫了,它現在只支持linux.
然後創建項目......
如果使用maven的話那就很簡單了,直接加入依賴
編輯pom.xml
- <repository>
- <id>spring-milestone</id>
- <name>Spring Maven MILESTONE Repository</name>
- <url>http://maven.springframework.org/milestone</url>
- </repository>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.0.RC1</version>
- </dependency>
- <repository>
- <id>spring-milestone</id>
- <name>Spring Maven MILESTONE Repository</name>
- <url>http://maven.springframework.org/milestone</url>
- </repository>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.0.RC1</version>
- </dependency>
如果沒有使用maven,那直接下載jar包http://s3.amazonaws.com/dist.springframework.org/release/DATAKV/spring-data-redis-1.0.0.RELEASE.zip
在spring配置文件裏添加
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="localhost"/>
- <property name="port" value="6636"/>
- </bean>
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"/>
- </bean>
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="localhost"/>
- <property name="port" value="6636"/>
- </bean>
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"/>
- </bean>
那就可以直接用redisTemplate,redisTemplate和HibernateTemplate很相似。
- @Service
- public class RedisService {
- @Resource
- private RedisTemplate<Serializable, Serializable> template;
- /**
- * 向redis裏面添加key-value格式的數據
- *
- * @param key key
- * @param value value
- */
- public void set(final Serializable key, final Serializable value) {
- template.execute(new RedisCallback<Object>() {
- @Override
- public Object doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] key_ = RedisUtil.getBytesFromObject(key);
- byte[] value_ = RedisUtil.getBytesFromObject(value);
- connection.set(key_, value_);
- return true;
- }
- });
- }
- /**
- * 根據key從redis裏面取出value
- *
- * @param key key
- */
- public Serializable get(final Serializable key) {
- return template.execute(new RedisCallback<Serializable>() {
- @Override
- public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] keyBytes = RedisUtil.getBytesFromObject(key);
- byte[] bytes = connection.get(keyBytes);
- return (Serializable) RedisUtil.getObjectFromBytes(bytes);
- }
- });
- }
- }
- @Service
- public class RedisService {
- @Resource
- private RedisTemplate<Serializable, Serializable> template;
- /**
- * 向redis裏面添加key-value格式的數據
- *
- * @param key key
- * @param value value
- */
- public void set(final Serializable key, final Serializable value) {
- template.execute(new RedisCallback<Object>() {
- @Override
- public Object doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] key_ = RedisUtil.getBytesFromObject(key);
- byte[] value_ = RedisUtil.getBytesFromObject(value);
- connection.set(key_, value_);
- return true;
- }
- });
- }
- /**
- * 根據key從redis裏面取出value
- *
- * @param key key
- */
- public Serializable get(final Serializable key) {
- return template.execute(new RedisCallback<Serializable>() {
- @Override
- public Serializable doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] keyBytes = RedisUtil.getBytesFromObject(key);
- byte[] bytes = connection.get(keyBytes);
- return (Serializable) RedisUtil.getObjectFromBytes(bytes);
- }
- });
- }
- }
看了一點JedisConnectionFactory,之際上它只是對Jedis做了下簡單了封裝,再加上自己的連接池實現。