[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 ...
随机推荐
- number_format
number_format — 以千位分隔符方式格式化一个数字 说明 string number_format ( float $number [, int $decimals = 0 ] ) str ...
- UVA 1292 十二 Strategic game
Strategic game Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- Apply Root Motion
Apply Root Motion 应用根动作: Should we control the character's position from the animation itself or fro ...
- can not find UIAutomationClient
'ClientApp.vshost.exe' (CLR v4.0.30319: ClientApp.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\asse ...
- PS(photoshop)里A4纸张的像素是多大?
A4纸的尺寸是210mm×297mm, 当你设定的分辨率是72像素/英寸时,A4纸的尺寸的图像的像素是595×842, 当你设定的分辨率是150像素/英寸时,A4纸的尺寸的图像的像素是1240×175 ...
- JBOSS批量扫描
exploit-db提供出了EXP,如下: /* * JBoss JMXInvokerServlet Remote Command Execution * JMXInvoker.java v0.3 - ...
- Spring AOP和AspectJ支持
学了Spring之后发现我都不知道java为何物-- 在这一章中有好几节,讲的切面编程 第一节:在项目中启用Spring的AspectJ注解支持 第二节:用AspectJ注解声明aspect 第三节: ...
- Codeforces Round #257 (Div. 2) B
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- HUD5423 Rikka with Tree(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5423 Rikka with Tree Time Limit: 2000/1000 MS (Java/O ...
- bootstrap学习笔记<九>(菜单,按钮。导航基本元素)
有了bootstrap作导航不再麻烦,几个样式,几个标签就能轻松搞定. 下面就来分解学习导航条的制作. 一.首先是下拉菜单 <div class="dropdown"> ...