package test.iafclub.redis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.iafclub.baseTools.util.MyDateUtil;
import com.iafclub.demo.domain.Dictionary; @RunWith(SpringJUnit4ClassRunner.class)
//配置了@ContextConfiguration注解并使用该注解的locations属性指明spring和配置文件之后,
//@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-activitymq-test.xml", "classpath:spring-mybatis.xml", "classpath:dubbo.xml" })
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-activitymq-test.xml", "classpath:spring-mybatis.xml", "classpath:dubbo.xml","classpath:spring-redis.xml" })
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate; private String REVERSE_KEY = "batchJob:task_";
private Dictionary initDictionary(String i){
Dictionary dictionary = new Dictionary();
dictionary.setId(UUID.randomUUID().toString());
dictionary.setTypeId("002");
dictionary.setTypeName("字典分类");
dictionary.setFieldKey("username"+i);
dictionary.setFieldValue("路人甲");
dictionary.setFieldBack("back1");
dictionary.setFieldBack2("back2");
dictionary.setFieldBack3("back3");
dictionary.setRemark("备注"+i);
return dictionary;
} /**Map插入*/
@Test
public void testMapPut(){
String key = REVERSE_KEY+"_testMap";
Map<String, String> newMap = new HashMap<String, String>();
stringRedisTemplate.boundHashOps(key).putAll(newMap);
BoundHashOperations<String, Object, Object> testMap = stringRedisTemplate.boundHashOps(key);
testMap.put("user2", JSONObject.fromObject(this.initDictionary("55555aa")).toString());
testMap.persist();
}
/**Map取值*/
@Test
public void testMapGet(){
String key = "testMap";
BoundHashOperations<String, Object, Object> testMap2 = stringRedisTemplate.boundHashOps(key);
System.out.println(testMap2.getExpire());
Dictionary dictionary = (Dictionary) JSONObject.toBean(JSONObject.fromObject(testMap2.get("user")), Dictionary.class);
System.out.println(dictionary.getFieldKey());
} /**String 类型插入*/
@Test
public void testStringPut(){
for (int i=0;i<10;i++){
Dictionary dictionary = this.initDictionary(i+"");
String messageContent = JSONObject.fromObject(dictionary).toString();
System.out.println("发送消息:" + messageContent);
String key = "USER_"+i;
stringRedisTemplate.boundValueOps(key).set(messageContent, 110,TimeUnit.DAYS);
} List<Dictionary> dictionarys = new ArrayList<Dictionary>();
for (int i=0;i<10;i++){ Dictionary dictionary = this.initDictionary(i+"");
dictionarys.add(dictionary);
}
String messageContent = JSONArray.fromObject(dictionarys).toString();
System.out.println("发送消息:" + messageContent);
BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps("dictionarysList");
opt.set(messageContent, 110,TimeUnit.SECONDS);
// opt.set(messageContent); System.out.println("发送完成");
} /**String 类型取值
* 获取指定的值*/
@Test
public void testStringGet(){
for (int i=0;i<1000;i++){
BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps("dictionarysList");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(opt.getExpire());
System.out.println(opt.get());
}
} /**String 类型取值
*获取所有USER_开头为key的值
* */
@Test
public void getSetValue(){
Set<String> keys = stringRedisTemplate.keys("USER_*");
System.out.println(keys);
for (String key : keys){
BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps(key);
System.out.println(opt.getExpire());
}
} /**删除
* */
@Test
public void deleteValue(){
// redisTemplate.delete("USER_*");
stringRedisTemplate.delete("dictionarysList");
System.out.println("0aaaaaaaaaaaaaaaaa");
} /**删除
* */
@Test
public void addValue(){
// redisTemplate.delete("USER_*");
stringRedisTemplate.boundValueOps("dictionarysList").set("aa", 2, TimeUnit.MINUTES);
System.out.println("0aaaaaaaaaaaaaaaaa");
} /**添加list
* */
@Test
public void testRedisTemplate(){
// redisTemplate.delete("USER_*");
String key = "boundListOps";
BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
System.out.println("=======a====="+boundListOperations);
boundListOperations.leftPush(this.initDictionary(System.currentTimeMillis()+""));
Object object = redisTemplate.boundListOps(key).leftPop();
System.out.println("=======b====="+JSONObject.fromObject(object)); System.out.println("\n\n\n\n\n\n\n\n\n\n");
} /**添加list
* */
@Test
public void testRedisTemplateB(){
String key = "mygod";
int i=0;
while(true){
i++;
BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
System.out.println("=======a====="+boundListOperations.size());
String keya = MyDateUtil.getCurrentDateTimeStr()+"00000000000"+i;
boundListOperations.leftPush(this.initDictionary(keya)); BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key+"Set");
boundSetOperations.add(this.initDictionary(keya)); if (i > 100){
break;
}
// Dictionary dictionary = (Dictionary) boundListOperations.rightPop();
// System.out.println(dictionary.getFieldKey());
}
System.out.println("\n\n\n\n\n\n\n\n\n\n");
}
@Test
public void testRedisListC(){
String key = "mygod";
BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
System.out.println(boundListOperations.size());
while (boundListOperations.size() > 0){
System.out.println(boundListOperations.rightPop());
}
} @Test
public void testRedisSet(){
String key = "mygodSet";
BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key);
System.out.println(boundSetOperations.size());
Set set = boundSetOperations.members();
for(Iterator itor = set.iterator();itor.hasNext();){
Dictionary d = (Dictionary) itor.next();
System.out.println(JSONObject.fromObject(d));
}
}
}

redis使用例子的更多相关文章

  1. Redis进阶例子

    工作中用到的RabbitMQ例子 , 但是最后没有用 , 用的CMQ , 顺便说下CMQ社区真的少 , 并且功能少 . 一.消息体 package com.bootdo.common.rabbitmq ...

  2. php操作redis简单例子

    <?php //在PHP里操作Redis //Redis就是php的一个功能类 //创建Redis对象 $redis = new Redis(); //链接redis服务器 $redis -&g ...

  3. Redis总结笔记(二):C#连接Redis简单例子

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/113.html?1455860686 注:C#在调用Redis是不要使用S ...

  4. 分割超大Redis数据库例子

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/124.html?1455853509 薄荷 App 上的伙伴功能大量使用了 ...

  5. 30个php操作redis常用方法代码例子

    From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...

  6. 30 个 php 操作 redis 常用方法代码例子

    这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类型. list 类型和 set 类型的数据 , 需要的朋友可以参 ...

  7. 爱漂泊人生 30个php操作redis常用方法代码例子

    http://www.justwinit.cn/post/8789/ 背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcach ...

  8. 30个php操作redis常用方法代码例子【转】

    背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcache使用,这是大才小用,这儿有30个方法来使用redis,值得了解. 这 ...

  9. php操作redis

    redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了.查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子.下面的例子都是基于php-redis这个扩展的. 1, ...

随机推荐

  1. Win10 改为用 Ctrl+Shift 切换中英输入语言而不是 Win+空格

    是切换中英输入语言,而不是切换输入法,如图: 步骤: 设置 > 设备 > 输入 > 高级键盘设置 > 语言栏选项 > 高级键盘设置 > 更改按键顺序 > 切换 ...

  2. C++ 日志库 boost::log 以及 glog 的对比

    日志能方便地诊断程序原因.统计程序运行数据,是大型软件系统必不可少的组件之一.本文将从设计上和功能上对比 C++ 语言常见的两款日志库: boost::log 和 google-glog . 设计 b ...

  3. LC 417. Linked List Cycle II

    题目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  4. python中浅拷贝和深拷贝分析

    首先,我们知道Python3中,有6个标准的数据类型,他们又分为可以变和不可变.不可变:Number(数字).String(字符串).Tuple(元组).可以变:List(列表).Dictionary ...

  5. Docker使用整理

    Docker技术的基础: namespace,容器隔离的基础,保证A容器看不到B容器. 6个名空间:User,Mnt,Network,UTS,IPC,Pid cgroups,容器资源统计和隔离.主要用 ...

  6. Angularjs 中 ng-repeat 循环绑定事件

    用ng-repeat循环是如果有ng-click之类的事件需要传入参数我们一般这样写 <span class='del' ng-click="RemoveCost({{item.Id} ...

  7. springboot启动流程(八)ioc容器refresh过程(下篇)

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 上一篇文章,我们知道了解析过程将从解析main方法所在的主类开始.在文章的最后我们稍微看 ...

  8. python多线程与多进程异步事件框架

    多线程简单实现 #!/usr/bin/env python # -*- coding: UTF-8 -*- import logging import queue import threading f ...

  9. Flutter学习之Dart语言基础(构造函数)

    最常见的构造函数形式,即生成构造函数,创建一个类的新实例: class Point { num x, y; //Dart中int和double是num的子类 //this引用当前类对象 Point(n ...

  10. 服务器去掉IE浏览器网络安全限制