1、Redis 的 Java API

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

jedis 依赖

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0</version>
  5. </dependency>

使用

  1. //连接 Redis
  2. Jedis jedis = new Jedis("localhost", 6379);
  3. //如果需要密码
  4. //jedis.auth("");
  5. //记录操作次数
  6. int i = 0;
  7. try {
  8. long start = System.currentTimeMillis();
  9. while (true) {
  10. long end = System.currentTimeMillis();
  11. //当 大于等于 1000毫秒(1秒)时,结束
  12. if (end - start >= 1000) {
  13. break;
  14. }
  15. i++;
  16. jedis.set("testId" + i, i + " ");
  17. }
  18. } finally {
  19. //关闭 Redis
  20. jedis.close();
  21. }
  22. //打印1秒内对 Redis 的操作次数
  23. System.out.println("Redis每秒操作:" + i + "次");

结果:

  1. Redis每秒操作:1753

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

  1. //配置 连接池
  2. JedisPoolConfig config = new JedisPoolConfig();
  3. //最大空闲数
  4. config.setMaxIdle(50);
  5. //最大连接数
  6. config.setMaxTotal(100);
  7. //最大等待数 毫秒数
  8. config.setMaxWaitMillis(20000);
  9. //创建 连接池
  10. JedisPool pool = new JedisPool(config,"localhost");
  11. //从连接池中获取单个连接
  12. Jedis jedis = pool.getResource();
  13. //如果需要密码
  14. //jedis.auth("");
  15. //记录操作次数
  16. int i = 0;
  17. try {
  18. long start = System.currentTimeMillis();
  19. while (true) {
  20. long end = System.currentTimeMillis();
  21. //当 大于等于 1000毫秒(1秒)时,结束
  22. if (end - start >= 1000) {
  23. break;
  24. }
  25. i++;
  26. jedis.set("testId" + i, i + " ");
  27. }
  28. } finally {
  29. //关闭 Redis
  30. jedis.close();
  31. }
  32. //打印1秒内对 Redis 的操作次数
  33. System.out.println("Redis每秒操作:" + i + "次");

运行结果:

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

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

spring-data-redis.jar 依赖包

  1. <dependency>
  2. <groupId>org.springframework.data</groupId>
  3. <artifactId>spring-data-redis</artifactId>
  4. <version>2.1.3.RELEASE</version>
  5. </dependency>

注解配置

  • 配置连接池

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

    • JredisConnectionFactory

    • JedisConnectionFactory

    • LettuceConnectionFactory

    • SrpConnectionFactory

  • 配置Spring RedisTemplate

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

使用 JedisConnectionFactory 较为广泛。

  1. @Configuration//声明当前类 是配置类
  2. public class SpringRedisConfig {
  3.  
  4. //配置连接池
  5. @Bean
  6. JedisPoolConfig poolConfig(){
  7. //配置连接池
  8. JedisPoolConfig config = new JedisPoolConfig();
  9. //最大空闲数
  10. config.setMaxIdle(50);
  11. //最大等待时间
  12. config.setMaxWaitMillis(20000);
  13. //最大连接数
  14. config.setMaxTotal(100);
  15. return config;
  16. }
  17.  
  18. //配置 redis 连接工厂
  19. @Bean
  20. RedisConnectionFactory connectionFactory(){
  21. JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig());
  22. return connectionFactory;
  23. }
  24.  
  25. //配置 Spring RedisTemplate
  26. @Bean
  27. StringRedisTemplate redisTemplate(){
  28. return new StringRedisTemplate(connectionFactory());
  29. }
  30. }

测试示例:

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

运行效果:

  1. zhe shi yi ge ce shi !

XML配置

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <!--配置连接池-->
  7. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  8. <!--最大等待时间-->
  9. <property name="maxWaitMillis" value="20000"/>
  10. <!--最大空闲数-->
  11. <property name="maxIdle" value="50"/>
  12. <!--最大连接数-->
  13. <property name="maxTotal" value="100"/>
  14. </bean>
  15. <!--Spring 提供的redis连接工厂-->
  16.  
  17. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  18. <property name="poolConfig" ref="poolConfig"/>
  19. </bean>
  20. <!--Spring Template-->
  21. <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  22. <property name="connectionFactory" ref="connectionFactory"/>
  23. </bean>
  24.  
  25. </beans>

测试类

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

运行效果:

  1. 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. How To:配置Openfiler磁盘

    目录 1.系统登陆 2.启用iSCSI Target服务 3.初始化卷组 4.映射LUN 5.查看状态 1.系统登陆 Openfiler系统安装完毕之后,使用提示的地址登陆:https://172.1 ...

  2. 网际协议IP简述

    最近花了些时间重新回顾了谢希仁教授主编的<计算机网络>关于网络层的章节,这是一本高校教材,里面关于计算机网络的内容比较基础,并且讲的很细致,笔者针对网际协议IP地址部分觉得有必要进行阅读后 ...

  3. [luogu4054 JSOI2009] 计数问题(2D BIT)

    传送门 Solution 2D BIT模板 Code //By Menteur_Hxy #include <cmath> #include <cstdio> #include ...

  4. LVM(Logical Volume Manager)逻辑卷管理

    本文实验部分,完全由本人亲自动手实践得来 文章中有部分的内容是我个人通过实验测试出来的,虽以目前本人的能力还没发现不通之处,但错误难免,所以若各位朋友发现什么错误,或有疑惑.更好的建议等,盼请各位能在 ...

  5. codeforeces近日题目小结

    题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...

  6. LINUX 查看硬件配置命令

      系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ...

  7. 2.1.5、SparkEnv中创建MapOutputTracker

    SparkEnv中创建MapOutputTracker def registerOrLookupEndpoint( name: String, endpointCreator: => RpcEn ...

  8. 09springMVC对ajax的支持

    u  最直接的Ajax处理 u  数据绑定@RequestBody/@ResponseBody u  使用@RequestBody/@ResponseBody来支持Ajax u  使用HttpEnti ...

  9. nyoj_114_某种序列_201403161700

    某种序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 数列A满足An = An-1 + An-2 + An-3, n >= 3 编写程序,给定A0, A1 ...

  10. JCE, Java Cryptography Extension

    JCE, Java Cryptography Extension Java 8 JCE下载地址: http://www.oracle.com/technetwork/java/javase/downl ...