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. kswapd和pdflush

    首 先,它们存在的目的不同,kswap的作用是管理内存,pdflush的作用是同步内存和磁盘,当然因为数据写入磁盘前可能会换存在内存,这些缓存真正写 入磁盘由三个原因趋势:1.用户要求缓存马上写入磁盘 ...

  2. 6.shell脚本

    6.1 shell基础语法 6.1.1 shell的概述 shell的基本概念 1.什么是shell shell是用户和Linux操作系统之间的接口,它提供了与操作系统之间的通讯方式 shell是一个 ...

  3. models中,字段参数limit_choices_to的用法

    这里,在使用 ModelForm 渲染前端页面的前提下,对于 models 中的 ManyToManyField 类型字段会在 ModelForm 中被转化为 ModelMultipleChoiceF ...

  4. Flask - 安装,仪式, 返回, 和 请求

    目录 Flask - 第一篇 安装,仪式, 返回, 和 请求 一. Flask 的安装 和 程序员仪式 安装 程序员仪式 二. Flask 的返回值 三. Flask 请求request Flask ...

  5. seminar information (Email template)

      The following is an email example of seminar information   **************** Dear all, It is a plea ...

  6. 洛谷—— P2015 二叉苹果树

    https://www.luogu.org/problem/show?pid=2015 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点 ...

  7. ArcGIS Engine 创建索引(属性索引)——提高查询效率

    转自原文 ArcGIS Engine 创建索引(属性索引)——提高查询效率 众所周知,建立索引可以提高查询的效率,当对FeatureClass中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...

  8. [Angulalr] Speed Up Reducer Development Using Ngrx Schematics

    When we use NGRX, we need to create some bolipates. Now with Angulalr6, we can use CLI to generate t ...

  9. Cocos2d-x 开发神器cococreator使用介绍

    Cocos2d-x 开发神器cococreator使用介绍 本篇博客小巫给大家推荐一个开发神器,你还在为搭建Cocos2d-x开发环境而头痛么.还在为平台移植问题而困扰么,我想大家都想更加高速得进行开 ...

  10. hihoCoder - 1079 - 离散化 (线段树 + 离散化)

    #1079 : 离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho在回国之后,又一次过起了朝7晚5的学生生活.当然了.他们还是在一直学习着各种算法 ...