`

jedis

阅读更多
public interface JRedisService {
	/**
	 * 
	 * @Description (删除键)
	 * @param key
	 * @date 2017年12月26日
	 */
	public void deleteKey(String... key);

	/**
	 * 
	 * @Description (判断key是否存在)
	 * @param key
	 * @return
	 * @date 2017年12月26日
	 */
	public boolean exists(String key);

	/**
	 * 
	 * @Description (设置key和value)
	 * @param key
	 * @param value
	 * @date 2017年12月26日
	 */
	public void set(String key, Object value);

	/**
	 * 
	 * @Description (设置key和value)
	 * @param key
	 * @param value
	 * @param isExpire
	 *            是否缓存
	 * @date 2018年10月20日
	 */
	public void set(String key, Object value, boolean isExpire);

	/**
	 * 
	 * @Description (设置key和value)
	 * @param key
	 * @param value
	 * @param seconds
	 *            缓存时间,单位秒
	 * @date 2018年10月20日
	 */
	public void set(String key, Object value, int seconds);

	/**
	 * 
	 * @Description (获取值)
	 * @param key
	 * @return
	 * @date 2017年12月26日
	 */
	public <T> T getBykey(String key);

	/**
	 * 
	 * @Description (设置String类型:key和value)
	 * @param key
	 * @param value
	 * @date 2018年11月15日
	 */
	public void setString(String key, String value);

	/**
	 * 
	 * @Description (设置String类型:key和value,是否缓存)
	 * @param key
	 * @param value
	 * @param isExpire
	 * @date 2018年11月15日
	 */
	public void setString(String key, String value, boolean isExpire);

	/**
	 * 
	 * @Description (设置String类型:key和value,设置时间)
	 * @param key
	 * @param value
	 * @param seconds
	 * @date 2018年11月15日
	 */
	public void setString(String key, String value, int seconds);

	/**
	 * 
	 * @Description (获取String值)
	 * @param key
	 * @return
	 * @date 2018年11月15日
	 */
	public String getSring(String key);

	/**
	 * 
	 * @Description (删除String值)
	 * @param key
	 * @date 2018年11月15日
	 */
	public void delString(String key);

}

 

import org.springframework.stereotype.Service;

import com.hesc.sifa.service.JRedisService;
import com.hesc.sifa.utils.JRedisUtils;
import com.hesc.sifa.utils.SerializeUtils;
import com.hesc.sifa.utils.StrToolUtils;
import com.hesc.trundle.message.Message;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

@Service("jRedisService")
public class JRedisServiceImpl implements JRedisService {

	// 单位是秒,默认10分钟
	private final static int EXPIRE = Integer.parseInt(Message.getMessage("redis.expire"));

	/**
	 * 
	 * @Description (删除键)
	 * @param key
	 * @date 2017年12月26日
	 */
	public void deleteKey(String... key) {
		Jedis jedis = JRedisUtils.getJedis();
		if (jedis != null) {
			try {
				jedis.del(key);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
	}

	/**
	 * 
	 * @Description (判断key是否存在)
	 * @param key
	 * @return
	 * @date 2017年12月26日
	 */
	public boolean exists(String key) {
		Jedis jedis = JRedisUtils.getJedis();
		boolean flag = false;
		if (jedis != null) {
			try {
				flag = jedis.exists(key);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
		return flag;
	}

	/**
	 * 
	 * @Description (设置key和value)
	 * @param key
	 * @param value
	 * @date 2017年12月26日
	 */
	public void set(String key, Object value) {
		set(key, value, true, EXPIRE);
	}

	public void set(String key, Object value, boolean isExpire) {
		set(key, value, isExpire, EXPIRE);
	}

	public void set(String key, Object value, int seconds) {
		set(key, value, true, seconds);
	}

	public void set(String key, Object value, boolean isExpire, int seconds) {
		Jedis jedis = JRedisUtils.getJedis();
		if (jedis != null) {
			try {
				jedis.set(key.getBytes(), SerializeUtils.serialize(value));
				if (isExpire) {
					jedis.expire(key.getBytes(), seconds);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
	}

	/**
	 * 
	 * @Description (获取值)
	 * @param key
	 * @return
	 * @date 2017年12月26日
	 */
	@SuppressWarnings("unchecked")
	public <T> T getBykey(String key) {
		Jedis jedis = JRedisUtils.getJedis();
		byte[] byt = null;
		if (jedis != null) {
			try {
				byt = jedis.get(key.getBytes());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
			if (null == byt) {
				return null;
			} else {
				return (T) SerializeUtils.unserialize(byt);
			}
		}
		return null;
	}

	/**
	 * 
	 * @Description (设置key和value)
	 * @param key
	 * @param value
	 * @date 2017年12月26日
	 */
	public void setString(String key, String value) {
		setString(key, value, true, EXPIRE);
	}

	public void setString(String key, String value, boolean isExpire) {
		setString(key, value, isExpire, 0);
	}

	public void setString(String key, String value, int seconds) {
		setString(key, value, true, seconds);
	}

	public void setString(String key, String value, boolean isExpire, int seconds) {
		Jedis jedis = JRedisUtils.getJedis();
		if (jedis != null) {
			try {
				Pipeline pipeline = jedis.pipelined();
				jedis.set(key, value);
				if (isExpire) {
					jedis.expire(key, seconds);
				}
				pipeline.sync();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
	}

	/**
	 * 
	 * @Description (获取值)
	 * @param key
	 * @return
	 * @date 2017年12月26日
	 */
	public String getSring(String key) {
		Jedis jedis = JRedisUtils.getJedis();
		String value = "";
		if (jedis != null) {
			try {
				value = jedis.get(key);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
			return StrToolUtils.isEmptyDefault(value);
		}
		return value;
	}

	/**
	 * 
	 * @Description (删除key)
	 * @param key
	 * @date 2018年11月15日
	 */
	public void delString(String key) {
		Jedis jedis = JRedisUtils.getJedis();
		if (jedis != null) {
			try {
				jedis.del(key);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
	}
}

 

import org.apache.log4j.Logger;

import com.hesc.trundle.message.Message;

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

public class JRedisUtils {
	private static final Logger log = Logger.getLogger(JRedisUtils.class);
	private static JedisPool jedisPool = null;
	// Redis服务器IP
	private static String ADDR = Message.getMessage("redis.addr");
	// Redis的端口号
	private static int PORT = Integer.parseInt(Message.getMessage("redis.port"));
	private static int MaxTotal = Integer.parseInt(Message.getMessage("redis.maxTotal"));
	private static int MaxWait = Integer.parseInt(Message.getMessage("redis.maxWait"));
	private static int MaxIdle = Integer.parseInt(Message.getMessage("redis.maxIdle"));

	static {
		initPool();
	}

	/**
	 * 
	 * @Description (初始化Redis连接池)
	 * @date 2017年12月26日
	 */
	private static synchronized void initPool() {
		try {
			if (jedisPool == null) {
				// 初始化非切片池
				jedisPool = new JedisPool(getJedisPoolConfig(), ADDR, PORT, MaxWait);
			}
		} catch (Exception e) {
			log.error(e);
		}
	}

	/**
	 * 
	 * @Description (获取配置)
	 * @return
	 * @date 2017年12月26日
	 */
	private static JedisPoolConfig getJedisPoolConfig() {
		JedisPoolConfig config = new JedisPoolConfig();
		// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
		config.setBlockWhenExhausted(true);
		// 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
		config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
		// 是否启用pool的jmx管理功能, 默认true
		config.setJmxEnabled(true);
		// 是否启用后今后出
		config.setLifo(true);
		// 最大连接数, 默认8个,为负数的时候没有限制。
		config.setMaxTotal(MaxTotal);
		// 最大空闲连接数, 默认8个,为负数的时候没有限制。
		config.setMaxIdle(MaxIdle);
		// 最小空闲连接数, 默认0
		// config.setMinIdle(0);
		// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
		config.setMaxWaitMillis(MaxWait);
		// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
		config.setTestOnBorrow(false);
		// 在还会给pool时,是否提前进行validate操作
		config.setTestOnReturn(false);
		// 自动测试池中的空闲连接是否都是可用连接
		config.setTestWhileIdle(true);
		// 逐出连接的最小空闲时间
		// config.setMinEvictableIdleTimeMillis(500);
		// 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出
		// config.setSoftMinEvictableIdleTimeMillis(500);
		// config.setTimeBetweenEvictionRunsMillis(30000);
		// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
		// config.setNumTestsPerEvictionRun(100);
		return config;
	}

	/**
	 * 获取Jedis实例
	 * 
	 * @return
	 */
	public synchronized static Jedis getJedis() {
		if (jedisPool == null) {
			initPool();
		}
		log.info("Redis活跃数=" + jedisPool.getNumActive() + "  空闲数=" + jedisPool.getNumIdle() + "  等待数="
				+ jedisPool.getNumWaiters() + "   jedisPool状态=" + jedisPool.isClosed());
		try {
			if (jedisPool.isClosed() || jedisPool.getNumActive() < 0) {
				log.info("*** 重新新建线程池jedisPool ***");
				jedisPool = null;
				initPool();
			}
			if (jedisPool != null) {
				log.info("+++ 获取Redis实例 +++");
				return jedisPool.getResource();
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return null;
		}
	}

	/**
	 * 释放jedis资源
	 * 
	 * @param jedis
	 */
	public synchronized static void close(final Jedis jedis) {
		try {
			if (jedis != null) {
				// 高版本采用close来归还
				log.info("jedis状态:" + jedis.isConnected());
				if (jedis.isConnected()) {
					log.info("--- 释放Redis实例 ---");
					jedis.close();
				}
			}

			// 活跃数超过一定的数量时,进行关闭重新创造线程池
			if (jedisPool.getNumActive() > MaxTotal - 100) {
				log.info("*** jedisPool.close() ***");
				jedisPool.close();
				jedisPool.destroy();
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			/*
			 * if (jedis.isConnected()) { jedis.quit(); jedis.disconnect(); }
			 */
			if (jedis != null) {
				// 高版本采用close来归还
				log.info("jedis状态:" + jedis.isConnected());
				if (jedis.isConnected()) {
					log.info("--- 释放Redis实例 ---");
					jedis.close();
				}
			}
			// jedisPool.close();
			// jedisPool.destroy();
		}

	}

	/**
	 * 
	 * @Description (清空库中所有数据,谨慎操作)
	 * @date 2017年12月26日
	 */
	public static void flushDatabase() {
		Jedis jedis = getJedis();
		if (jedis != null) {
			try {
				log.info("*** 清空Redis库中所有数据 ***");
				jedis.flushDB();
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e);
			} finally {
				close(jedis);
			}
		}
	}

	

	public static void main(String[] args) {
		Jedis jedis = JRedisUtils.getJedis();
		if (jedis != null) {
			try {
				//jedis.set("pxc", "pxc");
				//jedis.expire("pxc", 10);
				System.out.println(jedis.get("pxc"));
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JRedisUtils.close(jedis);
			}
		}
	}
}

 

分享到:
评论

相关推荐

    jedis-2.9.0.jar

    jedis-2.9.0.jar jedis-2.9.0 jar 包,不包含源码,源码下载地址: http://download.csdn.net/download/tan3739/9993938 测试代码: 导入依赖包: commons-lang-2.5.jar commons-pool2-2.4.2.jar jedis-2.9.0 jar ...

    jedis-2.8.0-API文档-中文版.zip

    赠送jar包:jedis-2.8.0.jar; 赠送原API文档:jedis-2.8.0-javadoc.jar; 赠送源代码:jedis-2.8.0-sources.jar; 赠送Maven依赖信息文件:jedis-2.8.0.pom; 包含翻译后的API文档:jedis-2.8.0-javadoc-API文档-...

    jedis-2.9.0-API文档-中文版.zip

    赠送jar包:jedis-2.9.0.jar; 赠送原API文档:jedis-2.9.0-javadoc.jar; 赠送源代码:jedis-2.9.0-sources.jar; 包含翻译后的API文档:jedis-2.9.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId...

    jedis-2.6.2.zip

    此资源为Java连接redis的工具jar,Jedis 版本为2.62; 有两个地方需要注意下;jedis 3.0新版本废弃了旧回收资源方法; 废弃方法为:JedisPool.returnResource(Jedis);回收资源 Jedis 3.0版本新回收资源: Jedis.close();...

    jedis-test.7z jedis 简单测试 项目工程

    jedis-test.7z jedis 简单测试 项目工程 lib 目录包含了依赖包: commons-lang-2.5.jar commons-pool2-2.4.2.jar jedis-2.9.0.jar 另: * Jedis 的源码工程(导入 eclipse 可直接运行): ...

    jedis-2.5.1.jar

    Jedis 是 Redis 官方首选的 Java 客户端开发包。 实例方法: ? 1 import redis.clients.jedis.* ? 1 2 3 Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo"); 支持...

    jedis-3.6.0-API文档-中文版.zip

    赠送jar包:jedis-3.6.0.jar; 赠送原API文档:jedis-3.6.0-javadoc.jar; 赠送源代码:jedis-3.6.0-sources.jar; 赠送Maven依赖信息文件:jedis-3.6.0.pom; 包含翻译后的API文档:jedis-3.6.0-javadoc-API文档-...

    jedis-3.0.1-API文档-中文版.zip

    赠送jar包:jedis-3.0.1.jar; 赠送原API文档:jedis-3.0.1-javadoc.jar; 赠送源代码:jedis-3.0.1-sources.jar; 赠送Maven依赖信息文件:jedis-3.0.1.pom; 包含翻译后的API文档:jedis-3.0.1-javadoc-API文档-...

    Redis.rar Jedis 读写效率测试 hgetAll hmset 10万数量级别测试

    测试Jedis在10万次读写级别情况下的读写效率。1)单 Jedis 读写1条命令,读写玩即close() 2) 单Jedis读写多条命令,此种情况尝试不释放Jedis连接,由于Jedis本质是tcp长连接,需要做异常判断  3)Pipeline方式...

    jedis-3.6.0-API文档-中英对照版.zip

    赠送jar包:jedis-3.6.0.jar; 赠送原API文档:jedis-3.6.0-javadoc.jar; 赠送源代码:jedis-3.6.0-sources.jar; 赠送Maven依赖信息文件:jedis-3.6.0.pom; 包含翻译后的API文档:jedis-3.6.0-javadoc-API文档-...

    jedis依赖jar包

    jedis依赖jar包, jedis-2.7.0.jar

    jedis相关jar包

    jedis相关jar包:commons-pool2-2.3.jar、jedis-2.7.0.jar

    jedis-2.9.0-API文档-中英对照版.zip

    赠送jar包:jedis-2.9.0.jar 赠送原API文档:jedis-2.9.0-javadoc.jar 赠送源代码:jedis-2.9.0-sources.jar 包含翻译后的API文档:jedis-2.9.0-javadoc-API文档-中文(简体)-英语-对照版.zip 对应Maven信息:...

    jedis-3.0.1-API文档-中英对照版.zip

    赠送jar包:jedis-3.0.1.jar; 赠送原API文档:jedis-3.0.1-javadoc.jar; 赠送源代码:jedis-3.0.1-sources.jar; 赠送Maven依赖信息文件:jedis-3.0.1.pom; 包含翻译后的API文档:jedis-3.0.1-javadoc-API文档-...

    使用redisson替代jedis

    使用redisson替代jedis中的的对应方法,以及对应的redis的命令在这三方中对应查询及操作,

    jedis jedis.jar

    jedis jar包; 版本:2.0 2.1

    jedis-2.8.0.jar

    jedis-2.8.0.jar

    Jedis API中文使用文档.-比较详细

    Java中使用Jedis操作Redis 文档比较详细, 具体的方法都进行了介绍了, 适合新手, 和老程序员进行复习, jedis api比较乱, 特此整理.

    jedisCluster 配置文件和调用Api

    对jedischangyongApi的一些简单封装和分类,全部标有中文注释,可直接放入项目中使用,jedis集群配置可参考 https://blog.csdn.net/qq_31256487/article/details/83144088;

    linux中安装redis和jedis及jedispool

    linux中安装redis和jedis及jedispool; redis安装所遇到的问题;

Global site tag (gtag.js) - Google Analytics