spring 结合 redis 例子 (转)
好了费话不多说了,介绍下spring 结合redis是怎么操作数据的 这里我用了maven管理,由于简单嘛,依赖下包就行了..不用单独去依赖包,成了我的习惯
好了,下面是pom的代码
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>redis</groupId>
- <artifactId>redis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <build>
- </build>
- <dependencies>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.0.2.RELEASE</version>
- </dependency>
- </dependencies>
- </project>
在pom里添加了redis spring客户端的依赖,那么所有的jar包都会帮你自动下载下来,是不是很方便啊,哈
下面是spring-context.xml代码
- <?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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <!--注解说明 -->
- <context:annotation-config />
- <!-- 把标记了@Controller注解的类转换为bean -->
- <context:component-scan base-package="com.mkfree.**" />
- <!-- redis工厂 -->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
- <!-- redis服务封装 -->
- <bean id="redisService" class="com.mkfree.redis.test.RedisService">
- </bean>
这里配置了一个跟spring 集成的redis客户端,ip port password自己定哦,这里我在redis配置文件了定义了需求密码认证,你们先不要用吧,为了简单起见
下面是RedisService,里面包含了对redis的方法,还有更多的方法没有去使用,这里当一个入门的小例子吧
- package com.mkfree.redis.test;
- import java.util.Set;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
- import redis.clients.jedis.Jedis;
- /**
- * 封装redis 缓存服务器服务接口
- * @author hk
- *
- * 2012-12-16 上午3:09:18
- */
- public class RedisService {
- /**
- * 通过key删除(字节)
- * @param key
- */
- public void del(byte [] key){
- this.getJedis().del(key);
- }
- /**
- * 通过key删除
- * @param key
- */
- public void del(String key){
- this.getJedis().del(key);
- }
- /**
- * 添加key value 并且设置存活时间(byte)
- * @param key
- * @param value
- * @param liveTime
- */
- public void set(byte [] key,byte [] value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
- /**
- * 添加key value 并且设置存活时间
- * @param key
- * @param value
- * @param liveTime
- */
- public void set(String key,String value,int liveTime){
- this.set(key, value);
- this.getJedis().expire(key, liveTime);
- }
- /**
- * 添加key value
- * @param key
- * @param value
- */
- public void set(String key,String value){
- this.getJedis().set(key, value);
- }
- /**添加key value (字节)(序列化)
- * @param key
- * @param value
- */
- public void set(byte [] key,byte [] value){
- this.getJedis().set(key, value);
- }
- /**
- * 获取redis value (String)
- * @param key
- * @return
- */
- public String get(String key){
- String value = this.getJedis().get(key);
- return value;
- }
- /**
- * 获取redis value (byte [] )(反序列化)
- * @param key
- * @return
- */
- public byte[] get(byte [] key){
- return this.getJedis().get(key);
- }
- /**
- * 通过正则匹配keys
- * @param pattern
- * @return
- */
- public Set<String> keys(String pattern){
- return this.getJedis().keys(pattern);
- }
- /**
- * 检查key是否已经存在
- * @param key
- * @return
- */
- public boolean exists(String key){
- return this.getJedis().exists(key);
- }
- /**
- * 清空redis 所有数据
- * @return
- */
- public String flushDB(){
- return this.getJedis().flushDB();
- }
- /**
- * 查看redis里有多少数据
- */
- public long dbSize(){
- return this.getJedis().dbSize();
- }
- /**
- * 检查是否连接成功
- * @return
- */
- public String ping(){
- return this.getJedis().ping();
- }
- /**
- * 获取一个jedis 客户端
- * @return
- */
- private Jedis getJedis(){
- if(jedis == null){
- return jedisConnectionFactory.getShardInfo().createResource();
- }
- return jedis;
- }
- private RedisService (){
- }
- //操作redis客户端
- private static Jedis jedis;
- @Autowired
- @Qualifier("jedisConnectionFactory")
- private JedisConnectionFactory jedisConnectionFactory;
- }
下面是测试代码TestRedis.java
- package com.mkfree.redis.test;
- import java.util.Set;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * redis spring 简单例子
- * @author hk
- *
- * 2012-12-22 上午10:40:15
- */
- public class TestRedis {
- public static void main(String[] args) throws InterruptedException {
- ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
- //这里已经配置好,属于一个redis的服务接口
- RedisService redisService = (RedisService) app.getBean("redisService");
- String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG
- System.out.println(ping);
- //首先,我们看下redis服务里是否有数据
- long dbSizeStart = redisService.dbSize();
- System.out.println(dbSizeStart);
- redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟)
- String username = redisService.get("username");//取值
- System.out.println(username);
- redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位)
- String username1 = redisService.get("username1");
- System.out.println(username1);
- Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间
- String liveUsername1 = redisService.get("username1");
- System.out.println(liveUsername1);//输出null
- //是否存在
- boolean exist = redisService.exists("username");
- System.out.println(exist);
- //查看keys
- Set<String> keys = redisService.keys("*");//这里查看所有的keys
- System.out.println(keys);//只有username username1(已经清空了)
- //删除
- redisService.set("username2", "oyhk2");
- String username2 = redisService.get("username2");
- System.out.println(username2);
- redisService.del("username2");
- String username2_2 = redisService.get("username2");
- System.out.println(username2_2);//如果为null,那么就是删除数据了
- //dbsize
- long dbSizeEnd = redisService.dbSize();
- System.out.println(dbSizeEnd);
- //清空reids所有数据
- //redisService.flushDB();
- }
- }
spring 结合 redis 例子 (转)的更多相关文章
- Spring Boot 2 + Redis例子
Redis是一个key-value数据库,支持存储的value类型包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).在 ...
- spring和redis的整合-超越昨天的自己系列(7)
超越昨天的自己系列(7) 扯淡: 最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三的,不过前提好像是研究的比较深,有了自己的见解.自认为学习 ...
- spring和redis的整合
spring和redis的整合-超越昨天的自己系列(7) 超越昨天的自己系列(7) 扯淡: 最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三 ...
- Spring Data Redis实现消息队列——发布/订阅模式
一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现. 定义:生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...
- Spring Data JPA例子[基于Spring Boot、Mysql]
关于Spring Data Spring社区的一个顶级工程,主要用于简化数据(关系型&非关系型)访问,如果我们使用Spring Data来开发程序的话,那么可以省去很多低级别的数据访问操作,如 ...
- spring配置redis注解缓存
前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...
- Spring Data Redis学习
本文是从为知笔记上复制过来的,懒得调整格式了,为知笔记版本是带格式的,内容也比这里全.点这里 为知笔记版本 Spring Data Redis 学习 Version 1.8.4.Release 前言 ...
- Spring Boot使用Spring Data Redis操作Redis(单机/集群)
说明:Spring Boot简化了Spring Data Redis的引入,只要引入spring-boot-starter-data-redis之后会自动下载相应的Spring Data Redis和 ...
- Spring Data Redis入门示例:基于Jedis及底层API (二)
使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...
随机推荐
- Yum与RPM
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...
- 阿里云ssh免密登陆突然无效
[root@node03 ~]# ssh-copy-id node02 root@node02's password: sh: .ssh/authorized_keys: Permission den ...
- 【和孩子一起学编程】 python笔记--第一天
[该随笔记录本人在阅读过程写的笔记和一些问题,格式比较随意,不定时更新] 由于该书使用的python版本为2.5,本人使用的为3.6. 第一章: 遇到的第一个问题: 1.3节 输出指令: print ...
- DMA实验总结
一.RCC设置 没什么好写的之前USART的基本一样 /************************************************************************ ...
- 【软工项目Beta阶段】第10周Scrum会议博客
第十周会议记录 小组GitHub项目地址https://github.com/ouc-softwareclass/OUC-Market 小组Issue地址https://github.com/ouc- ...
- zabbix历史数据全部清楚
#这种方法会出现 监控项不可用的情况 1.停掉zabbix_server 2.重重名表 RENAME TABLE history to history_20180117; RENAME TABLE h ...
- 树状数组,Fenwick Tree
Fenwick Tree, (also known as Binary Indexed Tree,二叉索引树), is a high-performance data structure to cal ...
- (转)使用OpenGL显示图像(二)定义Shapes
定义形状 编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/shapes.html 在一个OpenGL ES Vi ...
- php开发面试题---lavarel和tp的区别是什么(呕心整理)
php开发面试题---lavarel和tp的区别是什么(呕心整理) 一.总结 一句话总结: 反思的回顾非常有用,因为决定了我的方向和技巧 以战养己,这是非常非常好的方式 主要从大小.功能.安全性.操作 ...
- 一个python练习
问题描述: 有一对兔子,每隔3个月就生一对兔子,生下来的兔子也是每隔3个月就生兔子,以此类推... 用python模拟出来: #!/usr/bin/python3 import random impo ...