Java封装Redis常用操作
package com.advance.Redis; import org.apache.log4j.Logger;
import org.testng.annotations.Test;
import redis.clients.jedis.Jedis; import java.util.*; /**
* @Author: 谷天乐
* @Date: 2019/2/25 17:17
* @Gratitude: wudalang_gd
* @Description: 根据原生Jedis封装常用工具
*/
public class RedisUtil{
private static Logger logger = Logger.getLogger(RedisUtil.class);
private static final String ip = "localhost";
private static final Integer port = 6379;
//获取连接
public Jedis connection() throws Exception{
Jedis jedis = new Jedis(ip,port);
return jedis;
} //为string添加元素
public void set(String key, String value) throws Exception {
Jedis jedis = connection();
jedis.set(key,value); } //获取string
public String get(String key) throws Exception {
Jedis jedis = connection();
return jedis.get(key);
} //追加string
public void append(String key, String value) throws Exception {
Jedis jedis = connection();
jedis.append(key,value);
} //添加set
public void sadd(String key, Set<String> value) throws Exception {
Jedis jedis = connection();
for(String str: value){
jedis.sadd(key, str);
}
} //set删除指定元素
public void srem(String key, Set<String> value) throws Exception {
Jedis jedis = connection();
Iterator<String> it = value.iterator();
while(it.hasNext()){
String str = it.next();
jedis.srem(key, str);
}
} //获取key对应的value总数
public Long scard(String key) throws Exception {
Jedis jedis = connection();
return jedis.scard(key);
} //获取key对应的所有value
public Set<String> smembers(String key) throws Exception {
Jedis jedis = connection();
return jedis.smembers(key);
} //判断set是否存在
public boolean sismember(String key, String value) throws Exception {
Jedis jedis = connection();
return jedis.sismember(key,value);
} //随机获取数据
public String srandmember(String key) throws Exception {
Jedis jedis = connection();
return jedis.srandmember(key);
} //向list添加元素
public void lpush(String key, List<String> list) throws Exception {
Jedis jedis = connection();
for(String s: list){
jedis.lpush(key,s);
}
} //获取list
public List<String> lrange(String key, Integer start, Integer end)
throws Exception {
Jedis jedis = connection();
return jedis.lrange(key, start, end);
} //删除任意类型的key
public void del(String key) throws Exception {
Jedis jedis = connection();
jedis.del(key);
} //设置map
public void hmset(String key, Map<String, String> map) throws Exception {
Jedis jedis = connection();
jedis.hmset(key,map);
} //获取map的key的个数
public Long hlen(String key) throws Exception {
Jedis jedis = connection();
return jedis.hlen(key);
} //获取map中所有key
public Set<String> hkeys(String key) throws Exception {
Jedis jedis = connection();
return jedis.hkeys(key);
} //获取map中所有value
public List<String> hvals(String key) throws Exception {
Jedis jedis = connection();
return jedis.hvals(key);
} //获取map中的指定key的value
public List<String> hmget(String key, String... params)
throws Exception {
Jedis jedis = connection();
if (null == params || params.length == 0) {
throw new RuntimeException(this.getClass().getSimpleName()+ "::"
+ new Exception().getStackTrace()[0].getMethodName()+"参数不能为空");
}
return jedis.hmget(key,params);
} //获取map所有的key和value
public Map<String, String> hgetAll(String key)
throws Exception {
Jedis jedis = connection();
return jedis.hgetAll(key);
} //删除指定key的map
public void hdel(String key, String... params) throws Exception {
Jedis jedis = connection();
if (null == params || params.length == 0) {
throw new RuntimeException(this.getClass().getSimpleName()+ "::"
+ new Exception().getStackTrace()[0].getMethodName()+"参数不能为空");
}
jedis.hdel(key,params);
} //测试string
@Test
public void testString() throws Exception{
RedisUtil r = new RedisUtil();
r.set("z", "wb");
String s = r.get("z");
logger.debug(s);
} //测试set
@Test
public void testList() throws Exception{
RedisUtil r = new RedisUtil();
List<String> list = new ArrayList<>();
list.add("w");
list.add("b");
r.lpush("list",list);
List<String> t = r.lrange("list",0,-1);
logger.debug(t);
} //测试set
@Test
public void testSet() throws Exception{
RedisUtil r = new RedisUtil();
Set<String> set = new HashSet<String>();
set.add("w");
set.add("b");
r.sadd("set",set);
Set<String> t = r.smembers("set");
logger.debug(t);
} //测试map
@Test
public void mapTest() throws Exception {
RedisUtil r = new RedisUtil();
Map <String,String> map = new HashMap<>();
map.put("Red Alert 3","Long live Soviet");
map.put("Starcraft","No one can undie");
map.put("PUBG","Keep breath");
r.hmset("Game",map);
r.hdel("Game","Starcraft");
Map <String,String> m = r.hgetAll("Game");
logger.debug(m);
}
}
Java封装Redis常用操作的更多相关文章
- 【Redis使用系列】Redis常用操作
一.string类型的常用命令 set key value #一个key对应一个value.多次赋值,会覆盖前面. setnx key value #如果key存在则创建key1,并返回1,如果 ...
- Python Redis常用操作(持续更新)
目录 1.Redis简介 2.Redis部署 3.Redis API应用 4.String操作 1.Redis简介 redis是业界主流的key-value,nosql数据库之一.和Memcached ...
- java封装 redis 操作 对象,list集合 ,json串
/** * 功能说明: * 功能作者: * 创建日期: * 版权归属:每特教育|蚂蚁课堂所有 www.itmayiedu.com */package com.redis.service; import ...
- java中的redis常用操作
https://blog.csdn.net/lixiaoxiong55/article/details/81592800 超详细版 常规操作 public class TestReidsComm ...
- redis常用操作总结
在项目中时常会用到redis,redis看起来好像很难的样子,而且我也确认反复学习了很久,但是,总结下来,自己使用到的东西并不太多,如下作一些总结工作. 1.安装(单机) 1.1 windows, 直 ...
- Redis常用操作
一.string类型的常用命令 set key1 com #一个key对应一个value,多次复制,会覆盖前面的value setnx key1 zhangsan #如果key1不存在则创建key1, ...
- Redis常用操作大全和Python操作Redis
简单使用 utils.py import redis POOL=redis.ConnectionPool(host='127.0.0.1',port=6379) view.py 第一种方式 (通用方式 ...
- python openpyxl 封装Execl常用操作的方法
封装Excel操作方法:先装openpyxl:pip install openpyxl==2.4.5(可以指定版本) 封装脚本:#encoding=utf-8 from openpyxl import ...
- Redis 常用操作
import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;import java.util.Set; ...
随机推荐
- Java位操作全面总结[ZZ]
Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到四两拨千 ...
- ActiveMQ5.0实战三:使用Spring发送,消费topic和queue消息
实战一 , 实战二 介绍了ActiveMQ的基本概念和配置方式. 本篇将通过一个实例介绍使用spring发送,消费topic, queue类型消息的方法. 不懂topic和queue的google 之 ...
- Checkpoint--相关问题
Checkpoint是实例级别还是数据库级别? 答:数据库级别,在SQL Server关闭时,会对所有数据库逐一提交checkpoint 测试代码 USE DB0002 GO CHECKPOINT G ...
- 关于C_Sharp集中处理异常
1.写在前面 “异常意味着什么?”,想必不用对此做多余的解释,我们有理由相信在任何情况下任何应用程序都有可能出现异常,若在程序中没有对异常进行处理,则操作系统会以粗暴的方式处理掉它(弹出错误提示框), ...
- 网易云首席安全架构师谈安全新形势:DDOS两三天,游戏玩家数从几万降到几百
本文由 网易云发布. 安全是一个永恒的话题,在业务不断云化.攻击越来越复杂的当下,互联网安全呈现了出什么样的严峻形势?对这些形势,网易云又是如何应对的? 网易云首席安全架构师沈明星 4月13日,网易 ...
- CentOS 7 - 最小化安装以及引发的问题!
一,操作系统和虚拟机 操作系统:CentOS 7 官方网站:https://www.centos.org 下载地址:https://www.centos.org/download/ 下载版本分三个:D ...
- uiautomator2 手工翻译版
原文:https://github.com/openatx/uiautomator2 1.安装 pip install --pre uiautomator2 #或者你可以直接从github源码安装 ...
- Bit Manipulation-476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- Depth-first Search-690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 应该怎么理解 app = Flask(__name__)
初始化生成一个app对象,这个对象就是Flask的当前实例对象,后面的各个方法调用都是这个实例Flask会进行一系列自己的初始化,比如web API路径初始化,web资源加载,日志模块创建等.然后返回 ...