7、redis之使用spring集成commons-pool来操作常见数据类型
环境的搭建参见:http://www.cnblogs.com/yangzhilong/p/4729857.html
下面直接贴具体的测试代码:
package com.yzl; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.SerializationUtils; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; /**
* RedisApp的测试类
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisApp4Test {
private JedisPool pool;
private ApplicationContext app;
private Jedis jedis; @Before
public void before(){
app = new ClassPathXmlApplicationContext("spring-config.xml");
pool = app.getBean(JedisPool.class);
jedis = pool.getResource();
} @Test
public void test(){
//string
operationString(); //map
operationMap(); //list
operationList(); //自定义对象
operationMyObject();
} /**
*
* 功能描述: <br>
* 操作String
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationString(){
jedis.set("name", "hello");
jedis.append("name", " redis");
String value = jedis.get("name");
System.out.println("get value :" + value);
} /**
*
* 功能描述: <br>
* 操作Map
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationMap(){
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
jedis.hmset("map", map);
//这里可以一次读取多个key
List<String> result = jedis.hmget("map", "key1" , "key2");
for(String str : result){
System.out.println("get value from map : " + str);
}
} /**
*
* 功能描述: <br>
* 操作List
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationList(){
List<String> result = Arrays.asList("item1","item2");
for(String str : result){
jedis.lpush("list", str);
}
jedis.lpush("list", "value3");
result = jedis.lrange("list", 0, -1);
for(String str : result){
System.out.println("get value from list : " + str);
}
jedis.lpop("list");
for(String str : result){
System.out.println("get value from list again : " + str);
}
} /**
*
* 功能描述: <br>
* 操作自定义对象
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void operationMyObject(){
Entity entity = new Entity();
entity.setName("test");
entity.setAge(20);
//保存
jedis.set("obj".getBytes(), SerializationUtils.serialize(entity));
//读取
byte[] bytes = jedis.get("obj".getBytes());
Entity ret = (Entity)SerializationUtils.deserialize(bytes);
System.out.println("读取到自定义的类的属性name的值为:" + ret.getName());
} @After
public void after(){
//释放连接
pool.returnResourceObject(jedis);
System.out.println("end~~~");
}
}
运行的结果:
get value :hello redis
get value from map : value1
get value from map : value2
get value from list : value3
get value from list : item2
get value from list : item1
get value from list again : value3
get value from list again : item2
get value from list again : item1
读取到自定义的类的属性name的值为:test
end~~~
7、redis之使用spring集成commons-pool来操作常见数据类型的更多相关文章
- 【redis】3.Spring 集成注解 redis 项目配置使用
spring-data-redis 项目,配合 spring 特性并集成 Jedis 的一些命令和方法. 配置redis继承到spring管理项目,使用注解实现redis缓存功能. 参考:http: ...
- 5、redis之使用spring集成commons-pool
添加spring的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- spring集成Junit做单元测试及常见异常解决办法
spring-test依赖包 <!--Spring-test --> <!-- https://mvnrepository.com/artifact/org.springframew ...
- spring 集成redis客户端jedis(java)
spring集成jedis简单实例 jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...
- spring 集成 redis -- pub/sub
redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...
- Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置
Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- Redis 与 Spring 集成
配置applicationContext.xml <!-- 连接池配置 --> <bean id="jedisPoolConfig" class="re ...
随机推荐
- java实时监听日志写入kafka(转)
原文链接:http://www.sjsjw.com/kf_cloud/article/020376ABA013802.asp 目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kaf ...
- Java NIO AsynchronousFileChannel
In Java 7 the AsynchronousFileChannel was added to Java NIO. The AsynchronousFileChannel makes it po ...
- [转]教你修复win7中复制粘贴失效的问题
教你修复win7中复制粘贴失效的问题 发布时间:2018-01-17 使用win7系统的时候,我们经常需要对立面的内容进行复制粘贴来引用一些网站的内容,不过最近有网友在使用这个 ...
- go语言之进阶篇拷贝文件案例
1.文件案例:拷贝文件 示例: package main import ( "fmt" "io" "os" ) func main() { ...
- DevExpress ChartControl控件实现图表【转】
1.饼状图图 1.1添加ChartControl控件 在工具箱中找到ChartControl控件,拖到窗口中,创建Pie: 1.2准备数据 private DataTable CreateChartD ...
- python os.path模块常用方法详解 ZZ
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...
- eclipse如何debug调试jdk源码,并显示局部变量
http://blog.csdn.net/xuefeng0707/article/details/8738869 http://blog.csdn.net/leehsiao/article/detai ...
- bash shell seq的用法
seq 1 3 100 , 表示起始值为1, 间隔为3,终点值为100 #!/bin/bash aa=(1 2 3 17) #for i in 1 2 3 13 for i in ${aa[*]} d ...
- Dijkstra和Floyd_warshall
import java.util.Arrays; import java.util.Scanner; /*题目描写叙述: 有n个城市.城市间有m条道路.每条道路都有长度d.给你起点城市s终点终点t.要 ...
- 【Python】安装geopy
C:\Users\horn1\Desktop\python\49-geo>pip install geopy Collecting geopy Retrying (Retry(total=4, ...