Redis入門到精通只須要三篇博客

(Redis介紹:略)html

Redis-win-x64位程序的下載地址(缺分,分多的能夠給我貢獻點):java

http://download.csdn.net/download/qq_33601179/10165429linux

linux下的安裝百度一大堆,也不貼出來了,畢竟我沒用過,隨便貼一篇也不太好。redis

(1)win下安裝redis

很是簡單,只須要cmd命令行工具運行代碼,吧服務程序跑起來就OK,代碼爲:(親測有效)windows

redis-server.exe redis.windows.conf性能優化

具體的安裝步驟,請看博客:函數

http://blog.csdn.net/jinwufeiyang/article/details/52156817工具

(2)簡單使用Redis

這個我也不廢話,直接看下面這個博客:(親測有效)性能

http://www.runoob.com/redis/redis-java.html優化

(3)性能優化

Redis鏈接池,Redis自己就自帶鏈接池。(親測有效)

推薦將所須要的配置參數寫到配置文件中,下面附上我封裝的一個Redis鏈接池的類,由三個部分組成:類BaseRedis(使用Redis鏈接池:JedisPool)、類PropertiesUtils(用於讀取配置文件)、redis.properties(參數配置文件)

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Admin on 2017/12/19.
 */
public class BaseRedis {

    private static JedisPool pool;//redis鏈接池

    /**
     * 從鏈接池中獲取對象
     *
     * @return
     */
    public static Jedis getJedis() {
        /**
         * 斷定鏈接池是否已經初始化
         */
        if (pool == null) {
            PropertiesUtils propertiesUtils=new PropertiesUtils("redis.properties");
            /**
             * 從配置文件中讀取配置參數
             */
            Integer maxTotal=propertiesUtils.getValueForInt("maxTotal");
            if(maxTotal==null){
                maxTotal=100;
            }

            Integer maxWaitMillis=propertiesUtils.getValueForInt("maxWaitMillis");
            if(maxWaitMillis==null){
                maxWaitMillis=1000;
            }


            Integer maxIdle=propertiesUtils.getValueForInt("maxIdle");
            if(maxIdle==null){
                maxIdle=10;
            }


            Integer port=propertiesUtils.getValueForInt("port");
            if(port==null){
                port=6379;
            }

            String redisUrl=propertiesUtils.getValue("redisUrl");
            if(redisUrl==null){
                redisUrl="localhost";
            }

            // 創建鏈接池配置參數
            JedisPoolConfig config = new JedisPoolConfig();
            // 設置最大鏈接數
            config.setMaxTotal(maxTotal);
            // 設置最大阻塞時間,記住是毫秒數milliseconds
            config.setMaxWaitMillis(maxWaitMillis);
            // 設置空間鏈接
            config.setMaxIdle(maxIdle);
            // 建立鏈接池
            pool = new JedisPool(config, redisUrl, port);
        }
        return pool.getResource();
    }


}
import java.io.InputStream;
import java.util.*;

/**
 * 讀取配置文件中的屬性
 */
public class PropertiesUtils {
    private String filePath;

    /**
     * 構造函數須要傳入待讀取的配置文件的名稱
     *
     * @param propertiesFilePath
     */
    public PropertiesUtils(String propertiesFilePath) {
        // ../../這個是根據當前類所在的目錄相對定位到配置文件的路徑,具體按需修改
        this.filePath = "../../../../" + propertiesFilePath;
    }

    /**
     * 讀取指定的配置參數
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        String str = "";
        try {
            Properties pro = new Properties();
            InputStream ins = this.getClass().getResourceAsStream(filePath);
            pro.load(ins);
            str = pro.getProperty(key);
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 讀取指定的配置參數
     *
     * @param key
     * @return
     */
    public Integer getValueForInt(String key) {
        String str = getValue(key);
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 讀取全部的配置參數
     *
     * @return
     */
    public Map<String, String> getAllValue() {
        //讀取屬性文件a.properties
        Map<String, String> map = new HashMap<String, String>();
        try {
            Properties prop = new Properties();
            InputStream ins = this.getClass().getResourceAsStream(filePath);
            prop.load(ins);     ///加載屬性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = prop.getProperty(key);
                map.put(key, value);
            }
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
#redis配置文件
#URL
redisUrl=localhost
#端口號
port=6379
#最大鏈接數
maxTotal=100
#超時時間,單位毫秒
maxWaitMillis=1000
#最大xxx
maxIdle=10
相關文章
相關標籤/搜索