参考链接:

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. SqlSever基础 rtrim函数 除去字符串的右边的空格,左边中间的不管

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  2. Eclipse新建工程编译R cannot be resolved to a variable问题

    Eclipse新建工程编译R cannot be resolved to a variable问题 新建工程编译提示R cannot be resolved to a variable 图1 然后打开 ...

  3. Spring 框架 详解 (一)

    Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架 * 分层: * SUN提供的EE的三层结构:web层.业务层.数据访问层(持久层,集成层) * Struts2是 ...

  4. 拖拽碰撞效果,高级浏览器下全部搞定(ie6-8还没有搞定)

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. linux包的相关命令

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  6. python_way ,day11 进程

    if __name__ == '__main__': for i in range(10): p = Process(target=foo, args=(i,)) # p.daemon = True ...

  7. Threejs 使用的3D格式

    3D格式你可以通过任意软件导出(.3ds,dae等),但是threejs 无法使用, 1,http://www.blender.org/ 下载这款开源的3d软件 2,https://github.co ...

  8. mysql概要(九)字符集和校对集

    1.mysql 字符集有细致设置: 2.mysql字符处理机制是:数据库和客户端之间存在一个字符集转换器(后文简称转换器)将客户端字符编码(必须告诉服务端的)转换成一种中间编码的数据(可自定义的但保证 ...

  9. Codeforces 713D Animals and Puzzle

    题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...

  10. poj1927Area in Triangle

    链接 物理渣只能搜题解了.. 分三种情况 1.len>=a+b+c 2.len<内切圆半径 圆的面积是最大的 -->以len为周长的圆 3.看这篇http://blog.sina.c ...