参考链接:

redisTemplate 操作

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 实现的更多相关文章

  1. 4、redis之使用commons-pool

    增加池的配置文件redis-pool.properties: #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.poo ...

  2. 缓存工厂之Redis缓存

    这几天没有按照计划分享技术博文,主要是去医院了,这里一想到在医院经历的种种,我真的有话要说:医院里的医务人员曾经被吹捧为美丽+和蔼+可亲的天使,在经受5天左右相互接触后不得不让感慨:遇见的有些人员在挂 ...

  3. 剑指架构师系列-Redis安装与使用

    1.安装Redis 我们在VMware中安装CentOS 64位系统后,在用户目录下下载安装Redis. 下载redis目前最稳定版本也是功能最完善,集群支持最好并加入了sentinel(哨兵-高可用 ...

  4. Python 使用 Redis 操作

    1.redis简介 redis是一款开源免费的高性能key-value数据库,redis特点: 支持更多的数据类型:字符串(String).列表(List).哈希(Map).数字(Int).集合(Se ...

  5. Redis学习(一) —— 基本使用与原理

    一.数据结构 string Redis字符串是可修改字符串,在内存中以字节数组形式存在. 下面是string在源码中的定义,SDS(Simple Dynamic String) struct SDS& ...

  6. 笔记68 Redis数据库

    一.Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI ...

  7. python 结合redis 队列 做一个例子

    结合redis 队列 做了一个例子 #!/usr/bin/env python # coding: utf-8 # @Time : 2018/12/21 0021 13:57 # @Site : # ...

  8. 聚光灯下的熊猫TV技术架构演进

    2015年开始的百播大战,熊猫TV是其中比较特别的一员. 说熊猫TV是含着金钥匙出生的公子哥不为过.还未上线,就频频曝光,科技号,微博稿,站上风口浪尖.内测期间更是有不少淘宝店高价倒卖邀请码,光内测时 ...

  9. Laravel学习笔记之Session源码解析(中)

    说明:在上篇中学习了session的启动过程,主要分为两步,一是session的实例化,即\Illuminate\Session\Store的实例化:二是从session存储介质redis中读取id ...

随机推荐

  1. FastReport处理空日期

    [IIf([D.ApproveDate]==DateTime.MinValue,"",[D.ApproveDate])]

  2. 【转载】ogre内存管理

    原文:ogre内存管理 OGRE内存分配策略相关文件及简述 OGRE提供了自己的内存分配策略,甚至为STL容器提供了新的分配策略,相关文件及简述如下: OgreMemoryAllocatedObjec ...

  3. 关于float的说明

    关于float的说明     如图所示,蓝色的div和红色的div处于黑色的div(宽为500px)之中,其中蓝色的div设置了做浮动: 由图一(红色div的margin-top为10px,margi ...

  4. C#四则运算之策略模式

    Calculator.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  5. Mysql存储引擎

    存储引擎是什么 例如,如果你在研究大量的临时数据,你也许需要使用内存存储引擎.内存存储引擎能够在内存中存储所有的表格数据.又或者,你也许需要一个支持事务处理的数据库(以确保事务处理不成功时数据的回退能 ...

  6. poj 1410 线段相交判断

    http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  7. Hibernate之N+1问题

    什么是hibernate的N+1问题?先了解这样一个描述: 多个学生可以对应一个老师,所以student(n)---teacher(1).Stu类中有一个属性teacher.在hibernate配置文 ...

  8. POJ3009 Curling

    题目链接:http://poj.org/problem?id=3009 题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者 ...

  9. 如何设置table中<tr>和<td>的高度

    //-----------------自定义表格table的行和列的宽和高----------------------// 先设置一个样式 如下: <style type="text/ ...

  10. Iframe刷新父窗口的几种方式

    /*Iframe刷新父窗口的几种方式在iframe的子页面中,使用onload刷新父页面的时候,遇到了一些问题. 1.目前来说,测试成功,并且兼容IE6/7和FF的刷新方式. */ <scrip ...