redis使用例子
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使用例子的更多相关文章
- Redis进阶例子
工作中用到的RabbitMQ例子 , 但是最后没有用 , 用的CMQ , 顺便说下CMQ社区真的少 , 并且功能少 . 一.消息体 package com.bootdo.common.rabbitmq ...
- php操作redis简单例子
<?php //在PHP里操作Redis //Redis就是php的一个功能类 //创建Redis对象 $redis = new Redis(); //链接redis服务器 $redis -&g ...
- Redis总结笔记(二):C#连接Redis简单例子
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/113.html?1455860686 注:C#在调用Redis是不要使用S ...
- 分割超大Redis数据库例子
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/124.html?1455853509 薄荷 App 上的伙伴功能大量使用了 ...
- 30个php操作redis常用方法代码例子
From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...
- 30 个 php 操作 redis 常用方法代码例子
这篇文章主要介绍了 30 个 php 操作 redis 常用方法代码例子 , 本文其实不止 30 个方法 , 可以操作 string 类型. list 类型和 set 类型的数据 , 需要的朋友可以参 ...
- 爱漂泊人生 30个php操作redis常用方法代码例子
http://www.justwinit.cn/post/8789/ 背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcach ...
- 30个php操作redis常用方法代码例子【转】
背景:redis这个新产品在sns时很火,而memcache早就存在, 但redis提供出来的功能,好多网站均把它当memcache使用,这是大才小用,这儿有30个方法来使用redis,值得了解. 这 ...
- php操作redis
redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了.查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子.下面的例子都是基于php-redis这个扩展的. 1, ...
随机推荐
- 公式编辑器之 AxMath(18)
1. 使用教程 视频教程,一共有18集,每集都比较短. >> 视频教程链接:B站,速度快,清晰 2. 破解软件下载链接 >> 下载链接:复制链接到迅雷或IDM下载很快 3. M ...
- Python 第一式
@Codewars Python练习 question ** Dashatize it ** Given a number, return a string with dash'-'marks bef ...
- python之SQLite笔记
sqlite3 打开文件并创建游标 conn = sqlite3.connect('adressbook.db')c = conn.cursor() 连接对象:sqlite3.connect('数据文 ...
- 处理bugs心法
1. 前提 第一层:遇到异常首先必须告诉自己,冷静,不要慌.(一看到Bug就心慌,那么武功就施展不了了) 2. 入门级 第二层:遇到Bug,第一潜意识看输出异常的信息的(控制台输出,Junit输出,页 ...
- shell习题第23题:检测网卡流量
[题目要求] 写一个脚本,检测网卡流量并记录到日志,需要按照如下格式并一分钟统计一次(只需统计外网网卡,网卡名称eth0) 2019-06-07 1:11 eth0 input: 1000bps et ...
- angular 源码 <一> rotuerLinkActive
这几篇,查看angular 源码. rotuerLinkActive 是路由的样式设置. 它的值是 css 的一个类.或者几个类. 主要代码如下 @Input() set routerLinkActi ...
- gin框架封装自己的路由 ②
在一个项目中,我们会有很多路由,那么我们该如何更好的管理自己的路由,在多人协同的情况下可以更好的规范路由呢,我来说一下自己的做法 1.承接gin框架初识(先跑一个简单demo) ①,先创建一个cont ...
- MYSQL日期相关操作
*******MYSQL中取当前周/月/季/年的第一天与最后一天******* 当年第一天: SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 ...
- javascript经常用到的函数
trim函数: trim() lTrim() rTrim()校验字符串是否为空: checkIsNotEmpty(str ...
- 在nuxt项目中使用component组件
编写组件页面 1.在components下新建一个新建组件页面,如下所示 2.新建完成之后初始页面的代码如下所示: 3.下面从element-ui上找一个顶部导航菜单写到组件页面. <el-me ...