java鬼混笔记:redis 3、java下使用redis,自定义一个工具类RedisUtil
2017-12-05 20:08
471 查看
记记用java操作redis,我就直接写成一个工具使用,方法也就几个,做个demo,需要哪个再补补。目前是使用单个redis的,加上了连接池,后面再试试集群的redis和分布式的看看。。。上代码。。。
package com.example.demo.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public enum MyRedisUtils {
INSTANCE;
private JedisPool jedisPool;
private MyRedisUtils() {
// 连接池配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10000);// 最大连接数
config.setMaxIdle(2000);// 最大空闲连接数
config.setMaxWaitMillis(1000 * 100);// 获取连接最大等等时间
config.setTestOnBorrow(true);// 获取连接的时检查有效性
String ip = "127.0.0.1";
int port = 6379;
String password = "123";
int timeout = 100000;// 连接超时时间
jedisPool = new JedisPool(config, ip, port, timeout, password);
}
public Jedis getJedis() {
Jedis jedis = jedisPool.getResource();
return jedis;
}
public String set(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
return "-1";
} finally {
releaseResource(jedis);
}
}
/**
* 保存字符串,没有返回null
* @param key
* @return
*/
public String get(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return "-1";
} finally {
releaseResource(jedis);
}
}
/**
* 拼接,返回拼接后的长度
* @param key 目标的key
* @param value 要接在后面的value
* @return
*/
public Long append(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.append(key, value);
} catch (Exception e) {
e.printStackTrace();
return 0L;
} finally {
releaseResource(jedis);
}
}
/**
* 保存字符串并设置保存有效期,成功返回OK
* @param key
* @param value
* @param seconds
* @return
*/
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
releaseResource(jedis);
}
}
/**
* 清空当前库下的数据
* @return
*/
public String flushDB() {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.flushDB();
} catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
releaseResource(jedis);
}
}
/**
* 判断Key是否存在
* @param key
* @return
*/
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
releaseResource(jedis);
}
}
/**
* 判断多个Key是否存在,返回存在的数量
* @param keys
* @return
*/
public Long exists(String... keys) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(keys);
} catch (Exception e) {
e.printStackTrace();
return 0L;
} finally {
releaseResource(jedis);
}
}
// 等等。。。
public void releaseResource(Jedis jedis) {
if (jedis != null) {
jedis.close();// 资源回收
}
}
}
ok,先记这些先,后面再补上。
package com.example.demo.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public enum MyRedisUtils {
INSTANCE;
private JedisPool jedisPool;
private MyRedisUtils() {
// 连接池配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10000);// 最大连接数
config.setMaxIdle(2000);// 最大空闲连接数
config.setMaxWaitMillis(1000 * 100);// 获取连接最大等等时间
config.setTestOnBorrow(true);// 获取连接的时检查有效性
String ip = "127.0.0.1";
int port = 6379;
String password = "123";
int timeout = 100000;// 连接超时时间
jedisPool = new JedisPool(config, ip, port, timeout, password);
}
public Jedis getJedis() {
Jedis jedis = jedisPool.getResource();
return jedis;
}
public String set(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
return "-1";
} finally {
releaseResource(jedis);
}
}
/**
* 保存字符串,没有返回null
* @param key
* @return
*/
public String get(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return "-1";
} finally {
releaseResource(jedis);
}
}
/**
* 拼接,返回拼接后的长度
* @param key 目标的key
* @param value 要接在后面的value
* @return
*/
public Long append(String key, String value) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.append(key, value);
} catch (Exception e) {
e.printStackTrace();
return 0L;
} finally {
releaseResource(jedis);
}
}
/**
* 保存字符串并设置保存有效期,成功返回OK
* @param key
* @param value
* @param seconds
* @return
*/
public String setex(String key, String value, int seconds) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
releaseResource(jedis);
}
}
/**
* 清空当前库下的数据
* @return
*/
public String flushDB() {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.flushDB();
} catch (Exception e) {
e.printStackTrace();
return "0";
} finally {
releaseResource(jedis);
}
}
/**
* 判断Key是否存在
* @param key
* @return
*/
public Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
releaseResource(jedis);
}
}
/**
* 判断多个Key是否存在,返回存在的数量
* @param keys
* @return
*/
public Long exists(String... keys) {
Jedis jedis = null;
try {
jedis = getJedis();
return jedis.exists(keys);
} catch (Exception e) {
e.printStackTrace();
return 0L;
} finally {
releaseResource(jedis);
}
}
// 等等。。。
public void releaseResource(Jedis jedis) {
if (jedis != null) {
jedis.close();// 资源回收
}
}
}
ok,先记这些先,后面再补上。
相关文章推荐
- java小笔记----awt 使用自定义字体
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- Redis 学习笔记六 Java使用redis(测试环境:Win7)
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
- redis学习笔记---java操作redis,使用expire模拟指定时间段内限制ip访问的次数;
- java学习笔记之使用MyEclipse编写一个简单的登陆界(一)
- JAVA操作图片裁切与缩放的一个工具类,个人收藏使用
- [原创]java WEB学习笔记40:简单标签概述(背景,使用一个标签,标签库的API,SimpleTag接口,创建一个自定义的标签的步骤 和简单实践)
- 在用java进行极光推送的时候,需要的一个工具类: 使用的时候,直接调用 sendToRegistrationId 方法
- Java8使用lambda表达式写的一个List,Set,Map遍历工具类
- Redis学习笔记之二 :在Java项目中使用Redis
- 关于Flex使用Blazeds连接Java后台的一个注意笔记
- java 使用redis的工具类
- java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- redis集群使用Java工具类(Java jedis集群工具类)
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
- Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用
- Java基础笔记 – Annotation注解的介绍和使用 自定义注解
- iOS开发笔记22--使用XIB自定义一个UIView