1、Redis 的 Java API

Java 中 使用 Redis 工具,要先去 maven 仓库中,下载 jedis jar包

jedis 依赖

    <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

使用

 //连接 Redis
Jedis jedis = new Jedis("localhost", 6379);
//如果需要密码
//jedis.auth("");
//记录操作次数
int i = 0;
try {
long start = System.currentTimeMillis();
while (true) {
long end = System.currentTimeMillis();
//当 大于等于 1000毫秒(1秒)时,结束
if (end - start >= 1000) {
break;
}
i++;
jedis.set("testId" + i, i + " ");
}
} finally {
//关闭 Redis
jedis.close();
}
//打印1秒内对 Redis 的操作次数
System.out.println("Redis每秒操作:" + i + "次");

结果:

Redis每秒操作:1753次

使用 流水线技术( 连接池 ),提高速度。

//配置 连接池
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲数
config.setMaxIdle(50);
//最大连接数
config.setMaxTotal(100);
//最大等待数 毫秒数
config.setMaxWaitMillis(20000);
//创建 连接池
JedisPool pool = new JedisPool(config,"localhost");
//从连接池中获取单个连接
Jedis jedis = pool.getResource();
//如果需要密码
//jedis.auth("");
//记录操作次数
int i = 0;
try {
long start = System.currentTimeMillis();
while (true) {
long end = System.currentTimeMillis();
//当 大于等于 1000毫秒(1秒)时,结束
if (end - start >= 1000) {
break;
}
i++;
jedis.set("testId" + i, i + " ");
}
} finally {
//关闭 Redis
jedis.close();
}
//打印1秒内对 Redis 的操作次数
System.out.println("Redis每秒操作:" + i + "次");

运行结果:

Redis每秒操作:5022次
2、Spring 中 使用 Redis

在Spring中使用Redis,除了需要jedis.jar外,还需要 spring-data-redis.jar 的依赖架包

spring-data-redis.jar 依赖包

 <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

注解配置

  • 配置连接池

  • 配置Spring所提供的连接工厂

    • JredisConnectionFactory

    • JedisConnectionFactory

    • LettuceConnectionFactory

    • SrpConnectionFactory

  • 配置Spring RedisTemplate

Spring所提供的连接工厂,无论 如何它们都是接口 RedisConnectionFacory 的实现类

使用 JedisConnectionFactory 较为广泛。

@Configuration//声明当前类 是配置类
public class SpringRedisConfig { //配置连接池
@Bean
JedisPoolConfig poolConfig(){
//配置连接池
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲数
config.setMaxIdle(50);
//最大等待时间
config.setMaxWaitMillis(20000);
//最大连接数
config.setMaxTotal(100);
return config;
} //配置 redis 连接工厂
@Bean
RedisConnectionFactory connectionFactory(){
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig());
return connectionFactory;
} //配置 Spring RedisTemplate
@Bean
StringRedisTemplate redisTemplate(){
return new StringRedisTemplate(connectionFactory());
}
}

测试示例:

public static void main(String[] args) {
//扫描 spring 注解
AnnotationConfigApplicationContext bean = new AnnotationConfigApplicationContext(SpringRedisConfig.class);
// 得到 spring 容器 中 的类
StringRedisTemplate stringRedisTemplate =
(StringRedisTemplate) bean.getBean("redisTemplate");
//使用 SpringRedisTemplate
stringRedisTemplate.boundValueOps("test").set("zhe shi yi ge ce shi !");
System.out.println(stringRedisTemplate.boundValueOps("test").get());
}

运行效果:

zhe shi yi ge ce shi !

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大等待时间-->
<property name="maxWaitMillis" value="20000"/>
<!--最大空闲数-->
<property name="maxIdle" value="50"/>
<!--最大连接数-->
<property name="maxTotal" value="100"/>
</bean>
<!--Spring 提供的redis连接工厂--> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--Spring Template-->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean> </beans>

测试类

    public static void main(String[] args) {
//加载 配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("redisConfig.xml");
//从容器中 获取 一个 bean
StringRedisTemplate bean = (StringRedisTemplate) context.getBean("stringRedisTemplate");
bean.boundValueOps("test").set("zhe shi yi ge jian dan de ce shi ");
System.out.println(bean.boundValueOps("test").get());
}

运行效果:

zhe shi yi ge jian dan de ce shi 

Spring + Redis ( 简单使用)的更多相关文章

  1. redis之(二十一)redis之深入理解Spring Redis的使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  2. spring redis入门

    小二,上菜!!! 1. 虚拟机上安装redis服务 下载tar包,wget http://download.redis.io/releases/redis-2.8.19.tar.gz. 解压缩,tar ...

  3. 分布式缓存技术redis学习—— 深入理解Spring Redis的使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  4. 深入理解Spring Redis的使用 (七)、Spring Redis 使用 jackson序列化 以及 BaseDao代码

    之前在介绍Spring Redis进行存储的时候,都是通过RedisTemplate中的defaultSerializer,即JdkSerializationRedisSerializer.通过Jdk ...

  5. spring redis @Cacheable注解使用部分错误及无效原因

    spring redis @Cacheable注解使用部分错误及无效原因 说明:     spring项目用到redis注解无效,解决问题中遇到一堆BUG,各种搜索,看了许多错误解决方案一一测试,对于 ...

  6. 一篇文章带你了解NoSql数据库——Redis简单入门

    一篇文章带你了解NoSql数据库--Redis简单入门 Redis是一个基于内存的key-value结构数据库 我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储存信息 在本篇 ...

  7. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  8. Spring的简单demo

    ---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...

  9. spring+redis 集群下的操作

    文章就是记录一下工作当中的用到的点,与测试方法以备用,会不断更新. 配置文件spring-redis.xml: <?xml version="1.0" encoding=&q ...

随机推荐

  1. Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage

    ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage 1.返回顶部 1.1. pack ...

  2. 计蒜客 疑似病毒 (AC自动机 + 可达矩阵)

    链接 : Here! 背景 : 开始我同学是用 AC自动机 + DP 的方法来做这道题, 这道题的标签是 AC自动机, 动态规划, 矩阵, 按道理来说 AC自动机 + DP 应该是能过的, 但是他不幸 ...

  3. sqlalchemy子查询

    使用subquery() 要使用c来定位上一个子句的属性 s1 = session.query(m.a,m.b).filter().subquery() s2 = session.query(s1.c ...

  4. linux - redis基础

    目录 linux - redis基础 redis 源码编译安装 redis 数据结构 1. strings类型 2. list 类型 3. sets集合类型 有序集合 5. 哈希数据结构 centos ...

  5. C写的AES(ECB/PKCS5Padding)

    摘自POLARSSL #pragma once #define AES_ENCRYPT 1 #define AES_DECRYPT 0 struct aes_context { int nr; /*! ...

  6. PAT 1113 Integer Set Partition

    Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...

  7. const关键字作用

    const int a; int const a; const int *a; int * const a; int const * a const; /******/ 前两个的作用是一样,a是一个常 ...

  8. JDK(Java Development Kit)内置常用自带工具一览(转)

    注意:可能随着JDK的版本升级,工具也会随着增多. JDK(Java Development Kit)是Java程序员最核心的开发工具,没有之一. JDK是一个功能强大的Java开发套装,它不仅仅为我 ...

  9. Linux终止进程的工具kill/killall/pkill/xkill/skill用法区别(转)

    一. 终止进程的工具kill .killall.pkill.xkill 终止一个进程或终止一个正在运行的程序,一般是通过kill .killall.pkill.xkill等进行.比如一个程序已经死掉, ...

  10. device busy

    在mount的时候经常会有device busy,这通常是因为该目录被某个用户或者进程使用.这时候可以用如下命令: fuser mount point 来看一下该mount point被哪个进程占用. ...