您的位置:首页 > 数据库 > Redis

java实现redis数据库访问

2014-11-27 09:52 399 查看
<strong>redis是一个数据库,一个在内存中的非关系型数据库,那为啥要用它,他是key-value形式存储的 value有很多形式 string set list hash 等,jedis提供了java访问redis数据库的方法,不知道为啥csdn资源下载中redis那个demo还要10分,怎么好意思要10分呢,还要8分,怎么好意思要8分呢,分享是美德。</strong>
<strong>我先写下来,还没测试呢,不知道这么写会不会效率很低,因为每次调用方法都重新获取jedis对象,用完了释放掉,希望大家给出好的建议</strong>
package ***.**.**.**;import java.util.ResourceBundle;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;
/*** @类描述 redis 工具* @功能名 POJO* @author zxf* @date 2014年11月25日*/public final class RedisUtil {private static JedisPool jedisPool = null;/*** 初始化Redis连接池*/static {try {//加载redis配置文件ResourceBundle bundle = ResourceBundle.getBundle("redis");if (bundle == null) {throw new IllegalArgumentException("[redis.properties] is not found!");}int maxActivity = Integer.valueOf(bundle.getString("redis.pool.maxActive"));int maxIdle = Integer.valueOf(bundle.getString("redis.pool.maxIdle"));long maxWait = Long.valueOf(bundle.getString("redis.pool.maxWait"));boolean testOnBorrow = Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"));boolean onreturn = Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"));//创建jedis池配置实例JedisPoolConfig config = new JedisPoolConfig();//设置池配置项值config.setMaxActive(maxActivity);config.setMaxIdle(maxIdle);config.setMaxWait(maxWait);config.setTestOnBorrow(testOnBorrow);config.setTestOnReturn(onreturn);jedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));} catch (Exception e) {e.printStackTrace();}}/*** 获取Jedis实例* @return*/public synchronized static Jedis getJedis() {try {if (jedisPool != null) {Jedis resource = jedisPool.getResource();return resource;} else {return null;}} catch (Exception e) {e.printStackTrace();return null;}}/*** 释放jedis资源* @param jedis*/public static void returnResource(final Jedis jedis) {if (jedis != null) {jedisPool.returnResource(jedis);}}/*** 查询数据*/public String  find(String key,String value){Jedis jedis = null;try {jedis = jedisPool.getResource();return jedis.get(key);} catch (Exception e) {e.printStackTrace();return null;}finally{jedisPool.returnResource(jedis);}}/*** 查询特定字符串*/public String findSubStr(String key,Integer startOffset,Integer endOffset){Jedis jedis = null;try {jedis = jedisPool.getResource();return jedis.getrange(key, startOffset, endOffset);} catch (Exception e) {e.printStackTrace();return null;}finally{jedisPool.returnResource(jedis);}}/*** 向缓存中设置字符串内容 新增数据|修改* @param key key* @param value value* @return* @throws Exception*/public static int add(String key,String value) throws Exception{Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.set(key, value);return 0;} catch (Exception e) {e.printStackTrace();return -1;}finally{jedisPool.returnResource(jedis);}}/*** 删除缓存中得对象,根据key* @param key* @return*/public static int del(String key){Jedis jedis = null;try {jedis = jedisPool.getResource();jedis.del(key);return 0;} catch (Exception e) {e.printStackTrace();return -1;}finally{jedisPool.returnResource(jedis);}}}
用到的jar包
<img src="http://img.blog.csdn.net/20141127094609062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGl2ZXJzaXR5MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐