下载

依赖jar包下载

使用

# 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

redis.properties

package com.zze.util;

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);
    }

}

com.zze.util.PropertyUtil

package com.zze.util;

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();
        }
    }
}

com.zze.util.RedisClient

@Test
public void test2(){
    Jedis jedis = RedisClient.getJedis();
    jedis.set("name", "张三");
    String name = jedis.get("name");
    System.out.println(name);
}

java之jedis使用的更多相关文章

  1. Redis java client ==> Jedis

    https://github.com/xetorthio/jedis Jedis is a blazingly small and sane Redis java client. Jedis was ...

  2. 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍

    转载地址:http://blog.csdn.net/truong/article/details/46711045 关键字:Redis的Java客户端Jedis的八种调用方式(事务.管道.分布式…)介 ...

  3. Redis(九):Redis的Java客户端Jedis

    Redis的Java客户端Jedis导航目录: 安装JDK 安装Eclipse Jedis所需要的Jar包 Jedis常用操作 JedisPool 安装JDK tar -zxvf jdk-7u67-l ...

  4. 9.Redis的Java客户端Jedis

    Redis的Java客户端Jedis Jedis所需jar包   commons-pool-1.6.jar jedis-2.1.0.jar 1.Jedis常用操作(jedis中的api 和 我们在 l ...

  5. Redis入门和Java利用jedis操作redis

    Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...

  6. Java通过jedis操作redis缓存

    package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...

  7. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍

    jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...

  8. Java客户端Jedis的八种调用方式

      redis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分 ...

  9. 使用Redis的Java客户端Jedis

    转载自:http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 前一篇文章<Redis命令指南>讲解了通过命令行 ...

  10. [转载] 使用Redis的Java客户端Jedis

    转载自http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 在实际的项目开发中,各种语言是使用Redis的客户端库来与Re ...

随机推荐

  1. Spring Boot项目的接口防刷

    说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: import java.lang.an ...

  2. Mysql 索引问题-日期索引使用

    这两天发现原来的查询效率慢了,使用explain 查看,居然没有使用索引,我的索引是日期类型的,首先想到的是mysql对日期类型的索引的处理机制是不是不同,在where条件里试了几种,发现效果都差不多 ...

  3. redis跳跃表

    最近在阅读redis设计与实现,关于redis数据结构zset的一种底层实现跳跃表一直没有太理解,所以在搜了一下资料,终于搞懂了它的设计思路,记录一下. 参考链接:https://mp.weixin. ...

  4. iOS UITextView 设置 NSLinkAttributeName 属性,点击链接跳转

    @interface ViewController ()<UITextViewDelegate> - (void)viewDidLoad{    [super viewDidLoad];  ...

  5. Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded

    1.在build.gradle添加脚本指定编译堆内存 如果在整个工程中生效,则在build.gradle中增加如下配置: android { .............. dexOptions { i ...

  6. Spark性能优化指南-高级篇

    转自https://tech.meituan.com/spark-tuning-pro.html,感谢原作者的贡献 前言 继基础篇讲解了每个Spark开发人员都必须熟知的开发调优与资源调优之后,本文作 ...

  7. 微信开发时调用jssdk,在安卓设备中成功调用;在ios设备中返回错误消息:config fail,无其他具体错误消息,且接口权限显示获取ok,无法调用

    js代码如下: JavaScript code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  8. centos7修改系统时间、时区

    直接用下面命令直接更换时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  9. OpenGL矩阵变换,坐标空间变换

  10. Android学习:Notification状态栏通知

    Notification是显示在手机状态栏的通知,它代表一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification.在小米手机上,手指在屏幕顶端向下划 ...