侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

Jedis的简单使用

zze
zze
2017-10-18 / 0 评论 / 0 点赞 / 318 阅读 / 4061 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

1、关注文章首部微信公众号发送 #54_jedis 获取 jedis 依赖 jar 包。

2、创建属性配置文件:

# redis.properties
# Redis settings
redis.host=192.168.208.153
redis.port=6379
redis.pass=1234
redis.timeout=10000

redis.maxIdle=300
redis.maxTotal=600
# 毫秒
redis.maxWaitMillis=1000

3、工具类如下:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
/**
 * 属性文件加载工具类
 * @author zze
 */
public class PropertyUtil {

    //加载property文件到io流里面
    public static Properties loadProperties(String propertyFile) {
        Properties properties = new Properties();
        try {
            InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
            if (is == null) {
                is = PropertyUtil.class.getClassLoader().getResourceAsStream("properties/" + propertyFile);
            }
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 根据key值取得对应的value值
     *
     * @param key
     * @return
     */
    public static String getValue(String propertyFile, String key) {
        Properties properties = loadProperties(propertyFile);
        return properties.getProperty(key);
    }

}

4、获取 Redis 连接对象:

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

import java.util.Properties;
@SuppressWarnings("all")
public class RedisClient {
    //连接池
    private static JedisPool jedisPool;

    static {
        try {
            Properties properties = PropertyUtil.loadProperties("redis.properties");
            String host = properties.getProperty("redis.host");
            String port = properties.getProperty("redis.port");
            String pass = properties.getProperty("redis.pass");
            String timeout = properties.getProperty("redis.timeout");
            String maxIdle = properties.getProperty("redis.maxIdle");
            String maxTotal = properties.getProperty("redis.maxTotal");
            String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
            String testOnBorrow = properties.getProperty("redis.testOnBorrow");

            JedisPoolConfig config = new JedisPoolConfig();
            //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
            //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
            config.setMaxTotal(Integer.parseInt(maxTotal));
            //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
            config.setMaxIdle(Integer.parseInt(maxIdle));
            //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
            config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
            //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
            config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
            jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout), pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取Redis资源
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis jedis = jedisPool.getResource();
                return jedis;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //释放redis资源
    public synchronized static void releaseConn(Jedis jedis) {
        if (jedisPool != null) {
            jedis.close();
        }
    }
}

5、使用:

Jedis jedis = RedisClient.getJedis();
jedis.set("name", "张三");
String name = jedis.get("name");
System.out.println(name);
0

评论区