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, ...
随机推荐
- oddo
看了这张图,或许你对odoo有了一些兴趣. https://www.cnblogs.com/wterp/p/9093616.html 这次就是和大家一起交流开源ERP/CRM系统:odoo 对以下读者 ...
- Redis慢日志取出来
http://blog.chinaunix.net/uid-31396856-id-5758295.htmlhttps://blog.51cto.com/legehappy/2151986?sourc ...
- vue 中使用rem布局
在使用vue-cli搭建好项目框架后,在目录结构的index.html文件中添加一段js代码: fnResize(); window.onresize = function () { fnResize ...
- 本地Pycharm将spark程序发送到远端spark集群进行处理
前言 最近在搞hadoop+spark+python,所以就搭建了一个本地的hadoop环境,基础环境搭建地址hadoop2.7.7 分布式集群安装与配置,spark集群安装并集成到hadoop集群, ...
- 在oracle表中增加、修改、删除字段,表的重命名,字段顺序调整
增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...
- BZOJ4556 HEOI2016/TJOI2016字符串 (后缀树+主席树)
二分答案后相当于判断一个区间的后缀与某个后缀的最长公共前缀是否能>=ans.建出后缀树,在上述问题中后者所在节点向上倍增的跳至len>=ans的最高点,然后相当于查询子树中是否有该区间的节 ...
- IdentityServer4密码模式接入现有用户数据表
具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...
- SMTP命令
SMTP(Simple Mail Transfer Protocol)简单邮件传输协议 Basic Commands: HELO(Hello):标识用户身份 MAIL FROM:发件人地址 RCPT ...
- leetcode-29.两数相除(不用乘除法和mod)
如题,不用乘除法和mod实现两数相除. 这里引用一位clever boy 的解法. class Solution { public: int divide(int dividend, int divi ...
- HighChart 不同颜色(柱状图)
var chart = new Highcharts.Chart({ chart: { plotBackgroundColor: null, plotBorderWidth: null, backgr ...