Redis的windows版使用配置

Redis的windows版使用配置:java

下载:git

windows版https://github.com/MSOpenTech/redis/releases(微软开源) 或 https://github.com/dmajkic/redis/downloadsgithub

安装:redis

一、下载压缩包解压到文件夹redis中windows

二、在cmd中调用语句redis-server.exe redis.windows.conf (图一)服务器

三、新开cmd窗口调用语句redis-cli.exe (图二)maven

四、这时候能够进行调用redis操做了code

图一:server

图二:资源

 

Step 2:

在Java中使用redis须要用“redis客户端”,有jedis和jredis,还有在Spring中集成的Spring-data-redis等。

经过调用不一样的jar包便可使用,不过对redis的支持各有不一样。

这里使用jedis进行开发。

实例:JedisExample

package com.alvin.jedis;

import redis.clients.jedis.*;

/**
 * @Author: Alvin
 * @Project_name RedisExample
 * @Package_name: com.alvin.jedis
 * @Type_name JedisExample
 * @DATE: 2016/9/3
 * @TODO: java中使用redis的配置实例
 */
public class JedisExample{
    //Redis服务器IP
    private static final String IP = "localhost";
    //Redis的端口号
    private static final int PORT = 6379;
    //访问密码
    private static final String PASSWORD = null;
    //可用链接实例的最大数目,默认值为8;
    //若是赋值为-1,则表示不限制;若是pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
    private static final int MAX_ACTIVE = 1024;
    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8
    private static final int MAX_IDLE = 200;
    //等待可用链接的最大时间,单位毫秒,默认值为-1,表示永不超时。若是超过等待时间,则直接抛出JedisConnectionException;
    private static final int MAX_WAIT = 30000;
    private static final int TIME_OUT = 30000;
    //在borrow一个jedis实例时,是否提早进行validate操做;若是为true,则获得的jedis实例均是可用的;
    private static final boolean TEST_ON_BORROW = true;
    private static JedisPool jedisPool = null;

    /**
     * 初始化Redis链接池
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            //config.setMaxActive(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, IP, PORT, TIME_OUT, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Jedis实列
     */
    public synchronized static Jedis getJedis() {
        Jedis resouce = null;
        try {
            if (jedisPool != null) {
                resouce = jedisPool.getResource();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resouce;
    }

    /**
     * 释放jedis资源
     */
    public static void releaseJedis(final Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    public static void setString(String key, String value) {
        Jedis jedis = getJedis();
        try {
            jedis.set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }
    }

    /**
     * 根据key获取redis保存信息
     */
    public static String getString(String key) {
        Jedis jedis = getJedis();
        String result = null;
        try {
            result = getJedis().get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    public static void main(String[] args) {
        JedisExample.setString("name","Alvin");
        System.out.println(JedisExample.getString("name"));
    }
}

 

maven:

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.8.1</version>

</dependency>

 

Step 3:

API:http://doc.redisfans.com/