• redis缓存技术例子


    安装redis

    https://pan.baidu.com/s/1sTyVfGJ5PaZYXlRCUOJ00w

    https://www.runoob.com/redis/redis-install.html

    redis-server --service-install redis.windows.conf --loglevel verbose

    jar包

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.1.0-rc</version>
    </dependency>

    jedis(关键字)

    配置文件

    <?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="config" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxTotal" value="200"/>
        </bean>    
        
        <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
            <constructor-arg index="0" ref="config"/>
            <constructor-arg index="1">
                <list>
                    <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg index="0" value="127.0.0.1"/>
                        <constructor-arg index="1" value="6379"/>
                    </bean>
                    <!-- <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg index="0" value="192.168.1.138"/>
                        <constructor-arg index="1" value="6379"/>
                    </bean> -->
                </list>
            </constructor-arg>
        </bean>
        
    
    
    </beans>

    封装redisService类

    package com.mytaotao.common.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import redis.clients.jedis.ShardedJedisPool;
    
    @Service
    public class RedisService {
        @Autowired
        private ShardedJedisPool shardedJedisPool;
        
        /**
         * 保存数据到redis中
         */
        public void set(String key,String value) {
             shardedJedisPool.getResource().set(key, value);
        }
        /**
         * get数据
         */
        public String get(String key) {
             return shardedJedisPool.getResource().get(key);
        }
        
        /**
         * 删除
         */
        public void delete(String key) {
            shardedJedisPool.getResource().del(key);
        }
        
        /**
         * 设置有效时间
         */
        public void expire(String key,int time) {
            shardedJedisPool.getResource().expire(key, time);
        }
        
        /**
         * 
         */
        public void set(String key,String value,int time) {
            shardedJedisPool.getResource().setex(key, time, value);
        }
    
    }
  • 相关阅读:
    JavaScript深入之参数按值传递
    计算机网络:这是一份全面 & 详细 的TCP协议学习指南
    前端点击下载excel表格数据
    为什么选择器:last-child有时没有起作用?
    深入理解防抖和节流函数
    收集常用正则表达式
    深入研究-webkit-overflow-scrolling:touch及ios滚动
    一文搞懂网络知识,IP、子网掩码、网关、DNS、端口号
    正则替换replace中$1的用法
    数据库连接池性能对比
  • 原文地址:https://www.cnblogs.com/sh-0131/p/11743501.html
Copyright © 2020-2023  润新知