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, ...
随机推荐
- ElasticSearch入门-基本概念介绍以及安装
Elasticsearch基本概念 Elasticsearch是基于Lucene的全文检索库,本质也是存储数据,很多概念与传统关系型数据库类似. 传统关系型数据库与Elasticsearch进行概念对 ...
- Word 英语单词前面的汉字间距变大、Word 英文断词
1. 前言 在写文档时,在用word穿插敲汉字和英语时,会出现英语单词前面的汉字间的间距突然变大的情况,如何解决? 2. 步骤 1.选中间隙变大的段落,然后右键点击段落 2.然后点击中文版式,把允许西 ...
- LeetCode. 3的幂
题目要求: 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例: 输入: 27 输出: true 代码: class Solution { public: bool isPowerOfThr ...
- MySQL之mysqldump数据备份还原
一 mysqldump指令实现数据备份.mysql指令实现数据还原 经常有朋友问我,DBA到底是做什么的,百科上说:数据库管理员(Database Administrator,简称DBA),是从事管理 ...
- asp.net core-3.应用程序部署到iis
asp.net core在部署到iis 上的时候,iis服务器要装一个AspNetCoreModule,网站—>模块 具体下载地址可以去网上搜索https://www.nuget.org/pa ...
- zcat +文件名.gz | grep "查找内容"
linux gz查看 zcat +文件名.gz | grep "查找内容" 解压 rar x xxxx.rar
- MySQL SELECT表达式的执行顺序是从左往右依次执行
例子如下:(确保这几个变量都是初次使用,因为mysql的用户自定义变量会在整个连接session中存在) ,,; +--------+-------+---------+-------+ | +--- ...
- Bootstrap3 CDN 使用手册
一.一般功能 <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel=" ...
- vue 集成 vis-network 实现网络拓扑图
vis.js 网站 https://visjs.org/ vs code 下安装命令 npm install vis-network 在vue 下引入 vis-network组件 const v ...
- C# 第一次做项目。一些经验总结。
这是我的第一篇博客,写得不好望大家多多包涵. 初学C#2个多月,拿着老师给的项目,试着做了做,发现自己在编程方面有很多陋习与编程知识方面的不足. 首先是没有遵守某一个设计模式,这导致我想到哪里就做到了 ...