Redis入门到精通只需要三篇博客

(Redis介绍:略)

Redis-win-x64位程序的下载地址(缺分,分多的可以给我贡献点):

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

linux下的安装百度一大堆,也不贴出来了,毕竟我没用过,随便贴一篇也不太好。

(1)win下安装redis

非常简单,只需要cmd命令行工具运行代码,吧服务程序跑起来就OK,代码为:(亲测有效)

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

猜你喜欢

转载自my.oschina.net/u/1462828/blog/1592455