Redis是一个key-value存储系统。它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。为了保证效率,数据都是缓存在内存中。redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。以下是Jedis操作Redis与Spring的整合的单机版和集群版:

首先准备单击版和集群版的操作方法:JedisClientSingle、JedisClientCluster

import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.dao.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class JedisClientSingle { @Autowired
private JedisPool jedisPool; public String get(String key) {//获取指定 key 的值。如果 key 不存在,返回 nil 
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
} public String set(String key, String value) {//设置一些字符串值
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
} public String hget(String hkey, String key) {//获取哈希表中指定字段的值
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
} public long hset(String hkey, String key, String value) {//为哈希表中的字段赋值
Jedis jedis = jedisPool.getResource();
long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
} public long incr(String key) {//将 key 中储存的数字值增一,如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行INCR操作
Jedis jedis = jedisPool.getResource();
long result = jedis.incr(key);
jedis.close();
return result;
} public long expire(String key, int second) {//设置key的到期时间
Jedis jedis = jedisPool.getResource();
long result = jedis.expire(key, second);
jedis.close();
return result;
} public long ttl(String key) {//以秒为单位返回 key 的剩余过期时间
Jedis jedis = jedisPool.getResource();
long result = jedis.ttl(key);
jedis.close();
return result;
} public long del(String key) {//根据key删除
Jedis jedis = jedisPool.getResource();
long result = jedis.del(key);
jedis.close();
return result;
} public long hdel(String hkey, String key) {//删除哈希表key中的一个或多个指定字段
Jedis jedis = jedisPool.getResource();
long result = jedis.hdel(hkey, key);
jedis.close();
return result;
} }
import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.dao.JedisClient;

import redis.clients.jedis.JedisCluster;

public class JedisClientCluster {

    @Autowired
private JedisCluster jedisCluster; public String get(String key) {
return jedisCluster.get(key);
} public String set(String key, String value) {
// TODO Auto-generated method stub
return jedisCluster.set(key, value);
} public String hget(String hkey, String key) {
// TODO Auto-generated method stub
return jedisCluster.hget(hkey, key);
} public long hset(String hkey, String key, String value) {
// TODO Auto-generated method stub
return jedisCluster.hset(hkey, key, value);
} public long incr(String key) {
// TODO Auto-generated method stub
return jedisCluster.incr(key);
} public long expire(String key, int second) {
// TODO Auto-generated method stub
return jedisCluster.expire(key, second);
} public long ttl(String key) {
// TODO Auto-generated method stub
return jedisCluster.ttl(key);
} public long del(String key) {
// TODO Auto-generated method stub
return jedisCluster.del(key);
} public long hdel(String hkey, String key) {
// TODO Auto-generated method stub
return jedisCluster.hdel(hkey, key);
}

配置文件:applicationContext-jedis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- jedis单机版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<bean id="jedisClient" class="com.*.*.JedisClientSingle"></bean> <!-- jedis集群版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.242.135"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
</bean>
<bean id="jedisClientCluster" class="com.*.*.JedisClientCluster"></bean>
</beans>

完成以上步骤,使用JedisClientSingle、JedisClientCluster调用其中具体的方法进行操作

Java中Jedis操作Redis与Spring的整合的更多相关文章

  1. Redis入门和Java利用jedis操作redis

    Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...

  2. Java使用Jedis操作Redis大全

    Java操作Redis需要导入两个jar: commons-pool2-2.4.2.jar jedis-2.1.0.jar package com.chinasofti.test; import ja ...

  3. Java通过jedis操作redis(增删改查)

    package sgh.main.powersite; import java.util.ArrayList; import java.util.HashMap; import java.util.I ...

  4. Java通过jedis操作redis缓存

    package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...

  5. java客户端Jedis操作Redis Sentinel 连接池

    pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  6. jedis操作redis的几种常见方式总结

    Redis是一个著名的key-value存储系统,也是nosql中的最常见的一种,这篇文章主要给大家总结了关于在java中jedis操作redis的几种常见方式,文中给出了详细的示例代码供大家参考学习 ...

  7. JAVA中通过Jedis操作Redis连接与插入简单库

    一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...

  8. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  9. Jedis操作Redis数据库

    添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...

随机推荐

  1. js map、filter、forEach

    1.map方法 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...

  2. tcp/ip --IP:网际协议

    1.概述      IP是TCP/IP协议族中最为核心的协议.所有的TCP,UDP,ICMP,IGMP数据都以IP数据报格式传输.      IP提供不可靠,无连接的数据报传送服务. 不可靠:它不能保 ...

  3. 用Darwin开发RTSP级联server(拉模式转发)(附源代码)

    源代码下载地址:https://github.com/EasyDarwin orwww.easydarwin.org 在博客 在Darwin进行实时视频转发的两种模式 中,我们描写叙述了流媒体serv ...

  4. 写入文件,创建xunlei批量任务

    [php] <?php $arr = array(); $arr[] = 'http://s1.dwstatic.com/group1/M00/83/4A/834a040953745f52e8a ...

  5. Hive 练习 简单任务处理

    1.2018年4月份的用户数.订单量.销量.GMV (不局限与这些统计量,你也可以自己想一些) -- -- -- 2018年4月份的用户数量 select count(a.user_id) as us ...

  6. Mac 上的传奇效率神器 Alfred 3

    下载地址:https://www.alfredapp.com/ 第三方教程:https://www.jianshu.com/p/e9f3352c785f 一.内置快捷键介绍 1.默认快捷呼出热键是: ...

  7. JsCal( JS Calendar)

    http://www.dynarch.com/projects/calendar Doc: http://www.dynarch.com/jscal/ This is the documentatio ...

  8. Cap&#39;n Proto, FlatBuffers, and SBE

    转自:utm_source=tuicool">http://kentonv.github.io/capnproto/news/2014-06-17-capnproto-flatbuff ...

  9. FD_CLOEXEC用法及原因_转

    转自:使用FD_CLOEXEC实现close-on-exec,关闭子进程无用文件描述符 我们经常会碰到需要fork子进程的情况,而且子进程很可能会继续exec新的程序.这就不得不提到子进程中无用文件描 ...

  10. abp的权限与导航菜单的关系

    原来以为各是各的,所以就有了第一个版本.Getallmentus.然后注入了role,当然失败了.获取所有的菜单.一直在思考在什么地方设置菜单是否展示呢? 后面看了源码.才发现自己错了. UserNa ...