java web項目配置Redis

 

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同樣,爲了保證效率,數據都是緩存在內存中。java

 

安裝redis

環境準備:win 7環境、reids3.0web

步驟1:下載redis解壓到任意盤符,結果以下:redis

 

步驟2:點擊redis-server.exe啓動redis服務;spring

步驟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和項目配置的時候學的;但願能夠共同進步數據結構

相關文章
相關標籤/搜索