1. 程式人生 > >redis實戰(java專案應用)

redis實戰(java專案應用)

首先,簡單介紹下Redis,redis是一個key-value儲存系統,並提供多種語言的API。和Memcached類似,它支援儲存的value型別相對更多,包括string(字串)、list(連結串列)、set(集合)、zset(sorted set --有序集合)和hash(雜湊型別)。更詳細的介紹可以參照官網(點選開啟連結),本文主要介紹redis在工程專案中的具體應用(Redis的Java客戶端jedis的使用,貼出jedis原始碼地址:點選開啟連結)  下面是具體的步驟:  

1.下載jar包,本文使用的是jedis-2.5.2.jar,下載地址:http://grepcode.com/snapshot/repo1.maven.org/maven2/redis.clients/jedis/2.5.2現在很多專案使用maven作為專案依賴管理工具,maven只需在pom.xml檔案中新增以下依賴即可: redis.clients jedis 2.5.2   

2.下面即是配置類了,看程式碼:[java] view plain copy

package com.netease.lwt;  
  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.data.redis.connection.RedisConnectionFactory;  
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
import org.springframework.data.redis.core.StringRedisTemplate;  
  
import redis.clients.jedis.JedisPoolConfig;  
  
@Configuration  
@ComponentScan(basePackages="com.netease.lwt.dao")  
public class RedisConfiguration {  
      
    public static final String HOST = "e36d254038ee4529.m.cnhza.kvstore.aliyuncs.com";  
    public static final Integer PORT = 6379;  
    public static final String PASSWORD = "Laowantong2015";  
  
  
    @Bean  
    public RedisConnectionFactory jedisConnectionFactory(){  
       JedisPoolConfig poolConfig = new JedisPoolConfig();    
       poolConfig.setMaxIdle(8);  
       poolConfig.setMinIdle(0);  
       poolConfig.setTestOnBorrow(true);  
       poolConfig.setTestOnReturn(true);  
       poolConfig.setTestWhileIdle(true);  
       JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);  
       jedisConnectionFactory.setHostName(HOST);  
       jedisConnectionFactory.setPort(PORT);         
       jedisConnectionFactory.setPassword(PASSWORD);  
       return jedisConnectionFactory;  
    }  
      
    @Bean  
    public StringRedisTemplate redisTemplate(){  
       StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory());  
       return redisTemplate;  
    }  
}  
  


HOST,PORT,PASSWORD分別是伺服器地址,埠,連線密碼,其他的詳細配置可自己根據專案需求動態設定
  3.接下來就是資料結構的定義了,redis支援包括string,list,set,sorted set,hash等結構。本專案中只用到了Set,SortSet,HashSet,若需要使用redis其他資料結構,可另行定義類。
redis各資料結構所提供的方法,可參考: http://redisdoc.com/index.html。
   (1).redis資料集合名定義(set,get操作需要)
[java] view plain copy
public class RedisSetName {  
    // setName  
      public static final String USERGROUPS = "UserGroups";  
    public static final String GROUPAUCTIONS = "GroupAuctions";  
    public static final String AUCTIONJSONS = "AuctionJsons";  
    public static final String AUCTIONGOODS = "AuctionGoods";  
    public static final String AUCGOODIMAGES = "AucGoodImages";  
    public static final String GROUPINFOS = "GroupInfos";  
    public static final String USERAUCTIONISREMIND = "UserAuctionIsRemind";     
    public static final String USERFRIENDREMARK = "UserFriendRemark";     
    public static final String GROUPUSERS = "GroupUsers";    
    public static final String USERINFOS = "UserInfos";  
    public static final String RELUSERGROUPINFOS = "RelUserGroupInfos";  
    public static final String GROUPUSEREMARK = "GroupUserRemark";  
  
}  

    (2)下面就是dao層一些操作定義了。見具體程式碼:因程式碼貼出篇幅過大,下面就只給出sortedset的dao層定義,其他的資料結構操作類似。
     本文定義了redis中sortedset型別資料的增刪改查操作,可根據需要新增方法。

[java] view plain copy
package com.netease.lwt.dao;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.Set;  
import javax.inject.Inject;  
import org.springframework.data.redis.core.StringRedisTemplate;  
import org.springframework.stereotype.Repository;  
@Repository  
public class RedisSetRepository {  
     private StringRedisTemplate redisTemplate;  
    @Inject  
    public RedisSetRepository(StringRedisTemplate redisTemplate){  
       this.redisTemplate = redisTemplate;  
    }  
    public Long add(String setName, String value) {  
       Long num = redisTemplate.opsForSet().add(setName, value);  
       return num;  
    }  
    public Long add(String setName, List
values) { Long num = new Long(0); for(String value : values){ num += redisTemplate.opsForSet().add(setName, value); } return num; } public Long remove(String setName, String value){ Long num = redisTemplate.opsForSet().remove(setName, value); return num; } public void removeAll(String setName) { redisTemplate.opsForSet().remove(setName, redisTemplate.opsForSet().members(setName)); } public List
getAll(String setName){ setMembers = redisTemplate.opsForSet().members(setName); return new ArrayList(setMembers); } }    (3)接下來就可以在自己的Service層定義自己的方法了,比如你把使用者資訊快取了,那在Service中需要獲取使用者資訊的時候就可以通過上數dao層定義的方法上獲取了。下面貼出本專案中一個使用例項 [html] view plain copy /************************************** Redis相關增加/修改/刪除操作 **************************************************************/ @Override public void save(Group group) { groupRepository.save(group); redisHashSetRepository.set( RedisSetName.generateSetName(RedisSetName.GROUPINFOS, group.getIm()), "name", group.getName()); } public void deleteGroup(Group group) { groupRepository.deleteByIm(group.getIm()); redisHashSetRepository.remove( RedisSetName.generateSetName(RedisSetName.GROUPINFOS, group.getIm()), "name"); } [html] view plain copy /*************************************** Redis相關查詢操作**************************************************************/ @Override public List findGroupImsByUser(String uid) throws CustomException { return ims = redisSetRepository.getAll(RedisSetName.generateSetName(RedisSetName.USERGROUPS, uid)); }