zoukankan      html  css  js  c++  java
  • Spring + Redis ( 简单使用)

    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 
  • 相关阅读:
    BUCK/BOOST电路原理分析
    boost升压电路原理
    NPN/PNP和N沟道/P沟道负载的接法
    常用电源芯片记录
    LDO和BUCK降压稳压器对比
    some nets were not able to be matched
    Altium PCB布局时快速摆放元件的技巧
    树莓派+android things+实时音视频传输demo之遥控小车
    野狗产品与价格
    librtmp将本地FLV文件发布到RTMP流媒体服务器
  • 原文地址:https://www.cnblogs.com/chen1005/p/10515557.html
Copyright © 2011-2022 走看看