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/

相關文章
相關標籤/搜索