[redis] 普通 RedisPool 的 CRUD 实现
参考链接:
Maven中Spring-Data-Redis存储对象(redisTemplate)
1、配置RedisTempate类
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
属性文件
# Redis settings
redis.host=192.168.11.100
redis.port=6379
#redis.pass=hugsh redis.maxIdle=25
redis.maxTotal=250
#redis.maxActive=600 invalid in2.4
redis.maxWait=1000
redis.testOnBorrow=true
2、操作示例
1)创建User类
必须实现或者间接实现Serializable接口:
Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。
public class User implements Serializable { private static final long serialVersionUID = -7898194272883238670L; public static final String OBJECT_KEY = "USER"; public User() {
} public User(String id) {
} public User(String id, String name) {
this.id = id;
this.name = name;
} private String id; private String name; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} public String getKey() {
return getId();
} public String getObjectKey() {
return OBJECT_KEY;
}
}
2)创建userService类来操作redis增删查改缓存对象。
public class UserService { RedisTemplate<String, User> redisTemplate; public RedisTemplate<String, User> getRedisTemplate() {
return redisTemplate;
} public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
this.redisTemplate = redisTemplate;
} public void put(User user) {
redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
} public void delete(User key) {
redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
} public User get(User key) {
return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
}
}
3)配置service的bean
<bean id="userService" class="Service.UserService">
<property name="redisTemplate">
<ref bean="redisTemplate" />
</property>
</bean>
4)测试
public class Main {
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService"); User user1 = new User("user1ID", "User 1");
User user2 = new User("user2ID", "User 2"); System.out.println("==== getting objects from redis ====");
System.out.println("User is not in redis yet: " + userService.get(user1));
System.out.println("User is not in redis yet: " + userService.get(user2)); System.out.println("==== putting objects into redis ====");
userService.put(user1);
userService.put(user2); System.out.println("==== getting objects from redis ====");
System.out.println("User should be in redis yet: " + userService.get(user1));
System.out.println("User should be in redis yet: " + userService.get(user2)); System.out.println("==== deleting objects from redis ====");
userService.delete(user1);
userService.delete(user2); System.out.println("==== getting objects from redis ====");
System.out.println("User is not in redis yet: " + userService.get(user1));
System.out.println("User is not in redis yet: " + userService.get(user2)); }
}
5)结果
[redis] 普通 RedisPool 的 CRUD 实现的更多相关文章
- 4、redis之使用commons-pool
增加池的配置文件redis-pool.properties: #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.poo ...
- 缓存工厂之Redis缓存
这几天没有按照计划分享技术博文,主要是去医院了,这里一想到在医院经历的种种,我真的有话要说:医院里的医务人员曾经被吹捧为美丽+和蔼+可亲的天使,在经受5天左右相互接触后不得不让感慨:遇见的有些人员在挂 ...
- 剑指架构师系列-Redis安装与使用
1.安装Redis 我们在VMware中安装CentOS 64位系统后,在用户目录下下载安装Redis. 下载redis目前最稳定版本也是功能最完善,集群支持最好并加入了sentinel(哨兵-高可用 ...
- Python 使用 Redis 操作
1.redis简介 redis是一款开源免费的高性能key-value数据库,redis特点: 支持更多的数据类型:字符串(String).列表(List).哈希(Map).数字(Int).集合(Se ...
- Redis学习(一) —— 基本使用与原理
一.数据结构 string Redis字符串是可修改字符串,在内存中以字节数组形式存在. 下面是string在源码中的定义,SDS(Simple Dynamic String) struct SDS& ...
- 笔记68 Redis数据库
一.Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI ...
- python 结合redis 队列 做一个例子
结合redis 队列 做了一个例子 #!/usr/bin/env python # coding: utf-8 # @Time : 2018/12/21 0021 13:57 # @Site : # ...
- 聚光灯下的熊猫TV技术架构演进
2015年开始的百播大战,熊猫TV是其中比较特别的一员. 说熊猫TV是含着金钥匙出生的公子哥不为过.还未上线,就频频曝光,科技号,微博稿,站上风口浪尖.内测期间更是有不少淘宝店高价倒卖邀请码,光内测时 ...
- Laravel学习笔记之Session源码解析(中)
说明:在上篇中学习了session的启动过程,主要分为两步,一是session的实例化,即\Illuminate\Session\Store的实例化:二是从session存储介质redis中读取id ...
随机推荐
- Linux TOP命令详解
先来看一下执行以后的结果: 各项数值的含义如下: 第一行是任务队列信息. :: 当前时间 up days,: 系统运行时间,格式为天,时:分 user 当前登录用户数 load average: 0. ...
- XShell 安装与虚拟机连接
XShell:是liunx的远程管理工具 为啥要用这个工具呢?因为在古老的liunx字符命令下,是看不到中文的,要么使用liunx的图形化界面(支持中文),要么使用远程管理工具,是在windows中的 ...
- CUBRID学习笔记 3 net连接数据库并使用cubrid教程示例
接上文 数据库安装好后,也可以测试语句了. 下面我们用c#写一个控制台程序,连接数据库,并读取数据. 一 下载驱动 net版的下 CUBRID ADO.NET Data Provider 9.3.0 ...
- hdu 5587 规律
题意:开始序列{1}; 一次变换{1,1,2}: 两次变换{1,1,2,1,2,2,3} ... 求s[n];题解:打表 S1,S2,S4,S8,S16,S32......公式 S[n]=S[最近的比 ...
- git各种撤销操作
撤销git add: git reset HEAD+路径 或者git reset --+路径 撤销commit: 1.回退到具体的嘻哈值 git reset --hard 2.回退后仍包含本地 ...
- awt组件中文乱码Intellij解决
-Dfile.encoding=GB18030 -Dfile.encoding=GB18030
- (六)C语言之typedef详解
1.typedef可以看作type define的缩写,顾名思义就是类型定义,也就是说它只是给已有的类型重新定义了一个方便使用的别名,并没有产生新的数据类型.typedef的使用与宏定义define有 ...
- 关于协程的学习 & 线程栈默认10M
先看的这篇文章:http://blog.csdn.net/qq910894904/article/details/41699541 以nginx为代表的事件驱动的异步server正在横扫天下,那么事件 ...
- N-Queens II
Description: Follow up for N-Queens problem. Now, instead outputting board configurations, return th ...
- 转:设计模式-----桥接模式(Bridge Pattern)
转自:http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html 记得看原始链接的评论. 学习设计模式也有一段时间了,今天就把我整理 ...