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部分数据结构方法小结的更多相关文章

  1. php数组去重、魔术方法、redis常用数据结构及应用场景

    一.用函数对数组进行去重的方法 1.arrau_unique函数的作用 移除数组中重复的值. 将值作为字符串进行排序,然后保留每个值第一次出现的健名,健名保留不变. 第二个参数可以选择排序方式: SO ...

  2. Redis基本数据结构总结之STRING和LIST

    Redis基本数据结构总结前言 Redis的特点在于其读写速度特别快,因为是存储在内存中的,其非常适合于处理大数据量的情况:还有一个是其不同于其他的关系型数据库,Redis是非关系型数据库,也就是我们 ...

  3. Redis基本数据结构总结之SET、ZSET和HASH

    Redis基本数据结构总结 前言 Redis的特点在于其读写速度特别快,因为是存储在内存中的,其非常适合于处理大数据量的情况:还有一个是其不同于其他的关系型数据库,Redis是非关系型数据库,也就是我 ...

  4. Redis各种数据结构性能数据对比和性能优化实践

    很对不起大家,又是一篇乱序的文章,但是满满的干货,来源于实践,相信大家会有所收获.里面穿插一些感悟和生活故事,可以忽略不看.不过听大家普遍的反馈说这是其中最喜欢看的部分,好吧,就当学习之后轻松一下. ...

  5. 聊一聊Redis的数据结构

    如果没有记错的话,应该是在两个月前把 我们经常看到此类的文章: Redis的五种数据结构 Redis的数据结构以及对应的使用场景 其实以数据结构这个词去说明Redis的String.Hash.List ...

  6. Redis学习——数据结构介绍(四)

    一.简介 作为一款key-value 的NoSQL数据库,Redis支持的数据结构比较丰富,有:String(字符串) .List(列表) .Set(集合) .Hash(哈希) .Zset(有序集合) ...

  7. Redis学习笔记之Redis基本数据结构

    Redis基础数据结构 Redis有5种基本数据结构:String(字符串).list(列表).set(集合).hash(哈希).zset(有序集合) 字符串string 字符串类型是Redis的va ...

  8. [转]Redis内部数据结构详解-sds

    本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...

  9. redis内部数据结构深入浅出

    最大感受,无论从设计还是源码,Redis都尽量做到简单,其中运用到的原理也通俗易懂.特别是源码,简洁易读,真正做到clean and clear, 这篇文章以unstable分支的源码为基准,先从大体 ...

随机推荐

  1. bootstrap之消息提示

    <!DOCTYPE html><html>   <head>      <title>Bootstrap</title>      < ...

  2. C语言指针

    我是搞java的,今年要找工作,参加笔试就必须要准备一些C/C++.算法.数据结构.操作系统.概率论的东西,浅谈一下我作为初学者对C指针的学习和理解. 一.*p 概念: 申明一个整形指针变量: int ...

  3. jQuery实现滚动效果详解1

    声明:第一次写原创,本人初学,很多地方一知半解,本篇算是一个学习的笔记,欢迎批评指正,转载请注明. 今天要做的效果是在网上经常能看到多幅图片向左无缝滚动,鼠标滑过动画暂停,鼠标滑出动画继续的效果.网上 ...

  4. C#获取参数getParameter

    昨天遇到了这样一个问题,getParameter时,首次进入页面读取默认值时,本应该读取到“全部”这个字符. 然后在实际读取过程当中却不是这样,实际读取到的是  类的对象值 重新运行时能够读取到 “全 ...

  5. SSM框架学习之高并发秒杀业务--笔记1-- 项目的创建和依赖

    在慕课网上看了Java高并发秒杀API视屏后,觉得这个案例真的让我学到了很多,现在重新自己实现一遍,博客记下,顺便分析其中的要点. 第一步是项目的创建和依赖 利用Maven去创建工程然后导入Idea中 ...

  6. State(状态)

    props和state.props是在父组件中指定,而且一经指定,在被指定的组件的生命周期中则不再改变. 对于需要改变的数据,我们需要使用state.般来说,你需要在constructor中初始化st ...

  7. 三层交换单臂路由vlan间通信综合实验之降龙要点[转]

    单臂路由三层交换机提供vlan间的通信之菜鸟之降龙详解要点: 图示 PC:左到右依次设置IP172.16.10.1,    20.1,  30.1,   40,1  ,50,1  /24 网关10.2 ...

  8. 配置rc.local开机自启动文件的疑问

    有时我们自己在/etc/rc.d/rc.local里面增加的随机器启动的脚本和指令总是不能自动加载和启动,,机器启动后手动执行脚本又能成功,经常被搞得晕头转向的.最近我经过1天的辛苦测试和查找资料,终 ...

  9. python 从文件导入分类

    # -*- coding:utf-8 -*- """ 从文件导入分类 根据行首制表符或空格确定层级关系(4个空格等于一个制表符 同一行制表符和空格不能混用 ) 必须是 u ...

  10. composer -vvv

    然后在使用Composer install 或者 composer update 的时候会停住不动.使用-vvv可以输出更多信息,其命令参数输出的级别是Debug.具体可以查看composer hel ...