package com.daxin.jedis_datastructure;

import org.junit.After;
import org.junit.Before;
import org.junit.Test; import redis.clients.jedis.Jedis; /**
* Unit test for simple App.
*/
public class AppTest { Jedis jedis = null; @Before
public void before() {
jedis = RedisUtils.getJedis();
jedis.flushDB();
} @After
public void after() {
jedis.close();
} /**
* 简单key/value设置
*/
@Test
public void jedis_set_get() {
// 设置key
jedis.set("redis_key", "redis_value");
System.out.println(jedis.get("redis_key")); // 追加到指定key的后面
jedis.append("redis_key", "_redis_value");
System.out.println(jedis.get("redis_key")); // 1,2参数不解释
// NX如果不存在的话,则设置,否则不设置。XX如果存在则设置
// EX表示秒。PX表示毫秒
// 最后一个参数表示多长时间过期
jedis.set("redis_key", "123456789", "XX", "EX", 500L);
System.out.println(jedis.get("redis_key"));
jedis.set("123456789", "123456789", "NX", "EX", 500L);
System.out.println(jedis.get("123456789")); } /**
* redis中没有int类型,里面存储的是string,在进行int加减时候将string转int然后再转string存储
*/
@Test
public void jedis_incr_incrBy() { System.out.println("------------incrBy10------------");
// 加10
Long r1 = jedis.incrBy("top", 10);// redis中没有int类型,里面存储的是string,在进行int加减时候将string转int然后再转string存储
System.out.println(r1);
System.out.println("------------incr------------");
// 加1
r1 = jedis.incr("top");
System.out.println(r1); System.out.println("------------incrBy2------------");
r1 = jedis.incrBy("top", 2);
System.out.println(r1);
} @Test
public void jedis_decr_decrBy() { Long r1 = jedis.incrBy("top", 10); System.out.println(r1);
// 减1操作
r1 = jedis.decr("top");
System.out.println(r1);
// 减去4操作
r1 = jedis.decrBy("top", 4);
System.out.println(r1);
} @Test
public void jedis_getset() { /**
* 先获取在设置
*/
String r1 = jedis.getSet("daxin", "first");
System.out.println(r1);
r1 = jedis.getSet("daxin", "first");
System.out.println(r1); } @Test
public void jedis_setex() throws Exception {
String r1 = jedis.setex("loginstate", 5, "yes");
System.out.println(r1);//返回OK
System.out.println(jedis.get("loginstate"));
Thread.sleep(6000);//睡眠
System.out.println(jedis.get("loginstate"));//过期 }
/**
* 只有在 key 不存在时设置 key 的值。
* @throws Exception
*/
@Test
public void jedis_setnx() throws Exception {
//只有在 key 不存在时设置 key 的值。
Long r1 = jedis.setnx("top", "1");//返回值1设置ook, 0失败设置
System.out.println(r1);
r1 =jedis.setnx("top", "2");// 0失败设置
System.out.println(r1); } @Test
public void jedis_mget() {
//一次设置多个key/value,必须成对出现
String r1 = jedis.mset("daxin","first","la","laji");
System.out.println(r1);
System.out.println(jedis.get("daxin"));
System.out.println(jedis.get("la")); } /**
* 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始。
*/
@Test
public void jedis_range() {
jedis.set("top", "top-k");
jedis.setrange("top", 2, "*");//此处是覆盖,不是插入
System.out.println(jedis.get("top"));
} /**
* 返回 key 所储存的字符串值的长度。
*/
@Test
public void jedis_strlen() {
jedis.set("top", "top-k");
System.out.println(jedis.strlen("top"));
} /**
* 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
*/
@Test
public void jedis_msetnx() {
jedis.set("top", "top-k");
//注意:要所有的key都不存在才可以插入,否则全不插入
jedis.msetnx("top","toptop","111","1111");
System.out.println(jedis.get("top"));
System.out.println(jedis.get("111")); }
/**
* 这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。
* @throws Exception
*/
@SuppressWarnings("deprecation")
@Test
public void jedis_psetex() throws Exception {
jedis.psetex("top", 1000*60, "一分钟失效");
System.out.println(jedis.get("top"));
Thread.sleep(1000*60);
System.out.println(jedis.get("top"));
} }

最新文章

  1. chattr用法
  2. centeros:生产环境搭建
  3. jQuery原生框架-----------------属性操作
  4. 51Nod 1766 树上的最远点对
  5. SQL 数据库的使用
  6. mac 配置jdk maven
  7. cocos2d-x 开启深度测试验 实现 遮挡
  8. JS函数式编程【译】2.2 与函数共舞
  9. .Net 揭密--JIT怎样运行你的代码
  10. Java 十六进制转十进制
  11. JS实现定时器(类似工行网银支付限时操作)
  12. java 并发多线程异步
  13. WebForm 三级联动
  14. dmp文件的导入导出
  15. 获取页面定位元素left top
  16. 量化投资的Python库——Tushare
  17. Oracle 监听器日志解析
  18. MyBatis(二)最简易的增、删、改、查
  19. android打开存储卡(TF卡\SD卡)中的sqlite文件
  20. 启动mysql报错 -- ERROR! The server quit without updating PID file

热门文章

  1. [JSOI2010] 连通数
  2. 使用 libjpeg 库解压数据示例
  3. Vim 多行剪切、复制和删除
  4. Java并发编程:什么是CAS?这回总算知道了
  5. JavaSE Map集合
  6. JavaScript实现省市区的三级联动
  7. 【Java深入研究】4、fail-fast机制
  8. JDK动态代理与CGLib动态代理相关问题
  9. MEF 插件式开发之 WPF 初体验
  10. 解决Linux服务器tomact-8.0启动慢的问题