Redis部分数据结构方法小结
package com.practice.util; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class RedisUtil {
private static final Log log = LogFactory.getLog(RedisUtil.class); private static JedisPool jedisPool;//非切片连接池 private static final Object lock = new Object(); private static final int DEFAULT_TIME_OUT = 30000; private static String redisIp = "192.168.77.153"; private static Integer redisPort = 7000; /**
* 构建redis切片连接池
*
* @param ip
* @param port
* @return JedisPool
*/
public static JedisPool getJedisPool() {
if (jedisPool == null) {
synchronized (lock) {
if (jedisPool == null) {
JedisPoolConfig config = new JedisPoolConfig();
//设置连接池初始化大小和最大容量 // 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(-1); // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(1000);
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000 * 30);
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
// 写
jedisPool = new JedisPool(config, redisIp, redisPort,DEFAULT_TIME_OUT); }
}
}
return jedisPool;
} /**
* 返还到连接池
*
* @param pool
* @param redis
*/
public static void returnJedisResource(Jedis redis) {
if (redis != null) {
redis.close();
}
} //直接set key-value
public static void setStructure(String key,String value){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.set(key, value);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void getSetStructure(String key){
JedisPool pool = null;
Jedis jedis = null;
String value = "";
try {
pool = getJedisPool();
jedis = pool.getResource();
value = jedis.get(key);
System.out.println(value);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //通过key删除数据
public static void delKey(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.del(key);
System.out.println("del key success");
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //mset相当于 jedis.set("key1","value1");jedis.set("key2","value2")
public static void msetData(String key1,String value1,String key2,String value2){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.mset(key1,value1,key2,value2);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void flushData(){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.flushAll();
System.out.println("flushAll success");
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //判断key是否存在,如果存在则返回1,否则则返回0
public static boolean booleanExsit(String key){
JedisPool pool = null;
Jedis jedis = null;
Boolean exsit = false;
try {
pool = getJedisPool();
jedis = pool.getResource();
exsit = jedis.exists(key);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
return exsit;
} public static void appendData(String key,String data){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.append(key, data);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //截取value的值
public static void getRange(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
String value = jedis.getrange(key, 0, 1);
System.out.println(value);
System.out.println();
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //列表操作 用于将一个或多个值插入到列表的尾部(最右边), 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。
public static void rpush(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.rpush(key, "Hello how are you?");
jedis.rpush(key, "Fine thanks. I'm having fun with redis.");
jedis.rpush(key, "I should look into this NOSQL thing ASAP");
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} //取出列表中相应位置的值
public static void getPushValue(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
//第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有
List<String> values = jedis.lrange(key, 0, -1);
System.out.println(values);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void Set(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.sadd("myset", "1");
jedis.sadd("myset", "2");
jedis.sadd("myset", "3");
jedis.sadd("myset", "4");
Set<String> setValues = jedis.smembers("myset");
System.out.println(setValues);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void srem(String key){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.srem(key, "4");
Set<String> setValues = jedis.smembers("myset");
System.out.println(setValues);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void hmset(){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
Map<String, String> pairs = new HashMap<String, String>();
pairs.put("name", "Akshi");
pairs.put("age", "2");
pairs.put("sex", "Female");
jedis.hmset("kid", pairs);
List<String> name = jedis.hmget("kid", "name");
System.out.println(name);
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
} public static void increment(){
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getJedisPool();
jedis = pool.getResource();
jedis.hset("hashs", "entryKey", "1");
jedis.hset("hashs", "entryKey1", "entryValue1");
jedis.hset("hashs", "entryKey2", "entryValue2");
// 判断某个值是否存在
jedis.hexists("hashs", "entryKey");
System.out.println(jedis.hincrBy("hashs", "entryKey", 1));
} catch (Exception e) {
log.error(e);
} finally {
//返还到连接池
returnJedisResource(jedis);
}
}

Redis在工程开发中还是比较常用的Nosql内存数据库,简单巩固一下它的各种数据类型与用法~
Redis部分数据结构方法小结的更多相关文章
- php数组去重、魔术方法、redis常用数据结构及应用场景
一.用函数对数组进行去重的方法 1.arrau_unique函数的作用 移除数组中重复的值. 将值作为字符串进行排序,然后保留每个值第一次出现的健名,健名保留不变. 第二个参数可以选择排序方式: SO ...
- Redis基本数据结构总结之STRING和LIST
Redis基本数据结构总结前言 Redis的特点在于其读写速度特别快,因为是存储在内存中的,其非常适合于处理大数据量的情况:还有一个是其不同于其他的关系型数据库,Redis是非关系型数据库,也就是我们 ...
- Redis基本数据结构总结之SET、ZSET和HASH
Redis基本数据结构总结 前言 Redis的特点在于其读写速度特别快,因为是存储在内存中的,其非常适合于处理大数据量的情况:还有一个是其不同于其他的关系型数据库,Redis是非关系型数据库,也就是我 ...
- Redis各种数据结构性能数据对比和性能优化实践
很对不起大家,又是一篇乱序的文章,但是满满的干货,来源于实践,相信大家会有所收获.里面穿插一些感悟和生活故事,可以忽略不看.不过听大家普遍的反馈说这是其中最喜欢看的部分,好吧,就当学习之后轻松一下. ...
- 聊一聊Redis的数据结构
如果没有记错的话,应该是在两个月前把 我们经常看到此类的文章: Redis的五种数据结构 Redis的数据结构以及对应的使用场景 其实以数据结构这个词去说明Redis的String.Hash.List ...
- Redis学习——数据结构介绍(四)
一.简介 作为一款key-value 的NoSQL数据库,Redis支持的数据结构比较丰富,有:String(字符串) .List(列表) .Set(集合) .Hash(哈希) .Zset(有序集合) ...
- Redis学习笔记之Redis基本数据结构
Redis基础数据结构 Redis有5种基本数据结构:String(字符串).list(列表).set(集合).hash(哈希).zset(有序集合) 字符串string 字符串类型是Redis的va ...
- [转]Redis内部数据结构详解-sds
本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...
- redis内部数据结构深入浅出
最大感受,无论从设计还是源码,Redis都尽量做到简单,其中运用到的原理也通俗易懂.特别是源码,简洁易读,真正做到clean and clear, 这篇文章以unstable分支的源码为基准,先从大体 ...
随机推荐
- $.post 请求一直转圈圈,谷歌浏览器状态一直为canceled
最开始写的是 $.post("url",{},function(){},"json") 用火狐浏览器 测试发现请求一直在转圈圈 ,就在action输出 发现也进 ...
- asp.net页面跳转sessionid会变
今天发现在一个Asp.net站点中, 同一次登录, 不停刷新页面或者页面跳转, 此时后台Session的SessionID总是变化的. 创建一个页面,添加一个button,后台代码非常简单,如下 ...
- INTERSECT交集运算
INTERSECT交集是由既属于集合A,又属于集合B的所有元素组成的集合,如示意图1.
- ExtJS 刷新后,默认选中刷新前最后一次选中的节点
在对树节点进行操作后往往需要进行reload操作刷新一下树,但是很多业务都需要在树形刷新后默认选中最后一次选中的节点.这样就必须先保存前一次选中节点的信息,在reload之后再次通过节点的 ...
- 【转】ora-00031:session marked for kill处理oracle中杀不掉的锁
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- web前端基础篇⑧
1.伪类选择器 都以冒号开始.:focus 焦点的地方加样式:first-child 向元素的第一个子元素添加样式锚伪类:a:link {color:red} 未访问的链接 a:visited {co ...
- Android GridView的使用
Android的GridView控件用于把一系列的空间组织成一个二维的网格显示出来应用的比较多的就是组合图片显示下面我就详细讲一个例子 首先写一个类继承BaseAdapter 1. Java代码 1 ...
- tomcat一闪而过------Java EE环境部署
今天浪费了一个多钟头,tomcat一直一闪而过,最终原因让人哭笑不得,最后发现自己下载的是tomcat的源码版本....哎 部署环境步骤: 1.安装JDK 下载安装,JDK只需要配以下两个环境变量就可 ...
- 在 Xcode 6 中使用矢量图( iPhone 6 置配 UI)
在 Xcode 6 中使用矢量图( iPhone 6 置配 UI) (本文转载:http://iosdeveloper.diandian.com/post/2014-09-25/40063062789 ...
- 使用PHP处理文本小技巧
PHP的Cli模式使用:http://www.php.net/manual/zh/features.commandline.php PHP命令行部分参数:-B 在处理 stdin 之前先执行 ...