使用jedis簡單操作Redis

一、在項目中集成Jedis

	<!--集成redis-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.7.3</version>
    </dependency>

二、配置Redis的配置

# Redis settings
redis.host=redis服務的ip
redis.port=6379
#redis.pass=password
redis.dbIndex=0
redis.expiration=3000
#最大空閒數
redis.maxIdle=300
#最大連接數
redis.maxActive=600
#等待可用連接的最大時間,單位毫秒,默認值爲-1,表示永不超時
redis.maxWait=1000
redis.testOnBorrow=true

三、創建一個jedis工具類來操作Redis

package com.kude.util;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * @Author: 樊小銘
 * Date: 2019/7/23 13:06
 * @Version: 1.0
 * @Description: 爲了方便從Redis中存放數據生成的工具類,其中方法都可以通過類名靜態調用,使用懶漢單例模式
 *
 *      jedis工具api: http://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html
 */
public class JedisUtil {
    private static Jedis jedis = null;

    private static String filePath = "D:\\demo\\svn\\cloudBaby\\cloudBaby\\src\\main\\resources\\datasource.properties";
    private static long fileTiem = 0;

    private JedisUtil(){}

    private static Jedis getJedisUtil(){
        try {
            FileInputStream in = new FileInputStream(filePath);
            Properties properties = new Properties();
            properties.load(in);
            String host = properties.getProperty("redis.host");
            String port = properties.getProperty("redis.port");
            String max_wait = properties.getProperty("redis.maxWait");
            String maxIdle = properties.getProperty("redis.maxIdle");
            String maxActive = properties.getProperty("redis.maxActive");
            String testOnBorrow = properties.getProperty("redis.testOnBorrow");

            JedisPoolConfig config = new JedisPoolConfig();
            // 最大空閒
            config.setMaxIdle(Integer.parseInt(maxIdle));
            // 最長等待
            config.setMaxWaitMillis(Long.parseLong(max_wait));
            //
            config.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow));
            // 最大連接數
            config.setMaxTotal(Integer.parseInt(maxActive));

            JedisPool pool = new JedisPool(config,host, Integer.parseInt(port));


            jedis = pool.getResource();

            return jedis;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Jedis getJedis(){
        // jedis工具沒有初始化則初始化一下
        if(jedis == null){
            return getJedisUtil();
        }

        // 檢測配置文件是否發生改變,改變的話重新加載Redis
        File file = new File(filePath);
        if(file.lastModified() != fileTiem){
            fileTiem = file.lastModified();
            return getJedisUtil();
        }
        return jedis;
    }




    /**
     * 往Redis中存放內容
     * @param key Redis中的key
     * @param value
     */
    public static void addContent(String key,String value){
        // NX是不存在時才set, XX是存在時才set, EX是秒,PX是毫秒
        getJedis().set(key, value, "NX", "EX", 1000*60*30);
    }



    /**
     * 往Redis中存放內容
     * @param key Redis中的key
     * @param time 設置key的過期時間
     * @param value
     */
    public static void addContent(String key,String value,long time){
        // NX是不存在時才set, XX是存在時才set, EX是秒,PX是毫秒
        getJedis().set(key, value, "NX", "EX", time);
    }




    /**
     * 根據key得到值
     * @param key 存放數據的key
     * @return
     */
    public static String getValue(String key){
        return getJedis().get(key);
    }
}

jedis工具api: http://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章