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. java输入输出 -- java NIO之文件通道

    一.简介 通道是 Java NIO 的核心内容之一,在使用上,通道需和缓存类(ByteBuffer)配合完成读写等操作.与传统的流式 IO 中数据单向流动不同,通道中的数据可以双向流动.通道既可以读, ...

  2. laravel6.0路由

    1.基本路由路由定义在routes目录下,路由执行是在控制器之前,路由路径 routes目录下api.php 关于接口路由定义文件包含的路由位于 api 中间件组约束之内,支持频率限制功能,这些路由是 ...

  3. 注册中心Eureka 说明

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCloud将它集成在其子项 ...

  4. Python——类和对象(二)

    一.实例方法和自动绑定self 在类中定义的实例方法,Python会自动绑定方法的第一个参数(通常是self,下文也默认为self),第一个参数总会指向调用该方法的对象,因为实例方法(包括构造方法)第 ...

  5. 1186: 零起点学算法93——改革春风吹满地(C)

    一.题目 http://acm.wust.edu.cn/problem.php?id=1186&soj=0 二.分析 多组输入,'0'结束: 顶点的个数在3至100之间: 一定顺序输入坐标: ...

  6. easyui实现树形菜单Tab功能、layout布局

    一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...

  7. sql server 学习笔记 (nested transaction 嵌套事务)

    什么时候会用到嵌套事务 ? 为了代码复用,我们会写许多的储蓄过程,而中间如果需要使用到 transaction 难免就会发生嵌套了. sql server 并不直接支持嵌套事务. 但它可以用一些招式来 ...

  8. Http 与 Https区别

    传统Http协议弊端 传统Http协议弊端是明文的,如果别人采用抓包分析可以获取到明文数据. 什么是Https协议 HTTPS(Hyper Text Transfer Protocol over Se ...

  9. (十)SpringBoot之web 应用开发-Servlets, Filters, listeners

    一.需求 Web 开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet. FilterListene 二.案例 2.1 通过注册 ServletRegistr ...

  10. Python练习_Python初识_day2

    题目 1.作业 1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < ...