Redis缓存服务器

网址: http://www.redis.cn/

2.2.1 Redis介绍

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它能够用作数据库、缓存和消息中间件。 它反对多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 汇合(sets), 有序汇合(sorted sets) 与范畴查问, bitmaps, hyperloglogs 和 天文空间(geospatial) 索引半径查问。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘长久化(persistence), 并通过 Redis哨兵(Sentinel)和主动 分区(Cluster)提供高可用性(high availability)。
nginx: 3-5万/秒
redis: 读: 11.2万/秒 写: 8.6万/秒 均匀10万/秒
吞吐量: 50万/秒

2.2.2 Redis装置

1).解压redis文件

2).挪动文件/批改文件

3).装置Redis
命令1: make
命令2: make install

2.2.3 批改redis.conf配置文件

1.批改IP绑定

2.敞开保护模式

3.开启后盾启动

2.2.4 Redis命令

1.启动redis redis-server redis.conf

2.查看redis服务项

  1. 进入redis客户端
`redis-cli  -p 6379` *   1

4.敞开redis
1).命令 redis-cli -p 6379 shutdown
2).kill命令 kill -9 pid号

2.3 SpringBoot整合Redis

2.3.1 导入jar包

 `<!--spring整合redis -->        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>        </dependency>` *   1*   2*   3*   4*   5*   6*   7*   8*   9

2.3.2 入门案例

`package com.jt;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import redis.clients.jedis.Jedis;import redis.clients.jedis.params.SetParams;public class TestRedis {    /**     * 1.实现redis测试     * 报错查看:     *      1.查看redis.conf配置文件  1.ip绑定问题   2.保护模式问题  3.后盾启动问题     *      2.查看redis启动形式       redis-server redis.conf     *      3.查看防火墙     *      */    @Test    public void test01(){        Jedis jedis = new Jedis("192.168.126.129",6379);        jedis.set("2007", "redis入门案例");        System.out.println(jedis.get("2007"));    }    /**     * 我想判断是否有key数据,如果没有则新增数据,如果有则放弃新增     */    @Test    public void test02(){        Jedis jedis = new Jedis("192.168.126.129",6379);//        if(!jedis.exists("2007")){  //判断数据是否存在.//            jedis.set("2007", "测试案例2222");//        }        //setnx作用: 如果有数据,则不做解决.        jedis.setnx("2007", "测试高级用法");        System.out.println(jedis.get("2007"));    }    /**     * 需要:     *      向redis中增加一个数据.set-key-value,要求增加超时工夫 100秒.     * 暗藏bug: 代码执行过程中,如果报错,则可能删除失败.     * 原子性:  要么同时胜利,要不同时失败.     * 解决办法:  将入库操作/超时工夫一齐设定. setex     */    @Test    public void test03() throws InterruptedException {        Jedis jedis = new Jedis("192.168.126.129",6379);        //jedis.set("2007", "测试工夫");        //暗藏含意: 业务须要 到期删除数据        //jedis.expire("2007", 100);        jedis.setex("2007", 100, "测试工夫");        System.out.println(jedis.ttl("2007")+"秒");    }    /**     * 1.如果数据存在,则不操作数据  setnx     * 2.同时设定超时工夫,留神原子性 setex     * 参数阐明:     *   1.  XX = "xx";  只有key存在,则进行操作     *   2.  NX = "nx";  没有key,进行写操作     *   3.  PX = "px";  毫秒     *   4.  EX = "ex";  秒     */    @Test    public void test04() throws InterruptedException {        Jedis jedis = new Jedis("192.168.126.129",6379);        SetParams setParams = new SetParams();        setParams.xx().ex(100);        jedis.set("2007", "bbbbb",setParams);        System.out.println(jedis.get("2007"));    }}` *   1*   2*   3*   4*   5*   6*   7*   8*   9*   10*   11*   12*   13*   14*   15*   16*   17*   18*   19*   20*   21*   22*   23*   24*   25*   26*   27*   28*   29*   30*   31*   32*   33*   34*   35*   36*   37*   38*   39*   40*   41*   42*   43*   44*   45*   46*   47*   48*   49*   50*   51*   52*   53*   54*   55*   56*   57*   58*   59*   60*   61*   62*   63*   64*   65*   66*   67*   68*   69*   70*   71*   72

2.3.3 构建我的项目构造

AOP实现Redis缓存

5.1 现有代码存在的问题

1.如果间接将缓存业务,写到业务层中,如果未来的缓存代码发生变化,则代码耦合高,必然重写编辑代码.
2.如果其余的业务也须要缓存,则代码的反复率高,开发效率低.
解决方案: 采纳AOP形式实现缓存.

5.2 AOP

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译形式和运行期间动静代理实现程序性能的对立保护的一种技术。AOP是OOP的连续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP能够对业务逻辑的各个局部进行隔离,从而使得业务逻辑各局部之间的耦合度升高,进步程序的可重用性,同时进步了开发的效率。

5.3 AOP实现步骤

公式: AOP(切面) = 告诉办法(5种) + 切入点表达式(4种)

5.3.1 告诉温习

1.before告诉 在执行指标办法之前执行
2.afterReturning告诉 在指标办法执行之后执行
3.afterThrowing告诉 在指标办法执行之后报错时执行
4.after告诉 无论什么时候程序执行实现都要执行的告诉

上述的4大告诉类型,不能控制目标办法是否执行.个别用来记录程序的执行的状态.
个别利用与监控的操作.

5.around告诉(性能最为弱小的) 在指标办法执行前后执行.
因为盘绕告诉能够控制目标办法是否执行.管制程序的执行的轨迹.

5.3.2 切入点表达式

1.bean(“bean的ID”) 粒度: 粗粒度 按bean匹配 以后bean中的办法都会执行告诉.
2.within(“包名.类名”) 粒度: 粗粒度 能够匹配多个类
3.execution(“返回值类型 包名.类名.办法名(参数列表)”) 粒度: 细粒度 办法参数级别
4.@annotation(“包名.类名”) 粒度:细粒度 依照注解匹配

5.3.3 AOP入门案例

`package com.jt.aop;import lombok.extern.apachecommons.CommonsLog;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Service;import java.util.Arrays;@Aspect //标识我是一个切面@Component  //交给Spring容器治理public class CacheAOP {    //切面 = 切入点表达式 + 告诉办法    //@Pointcut("bean(itemCatServiceImpl)")    //@Pointcut("within(com.jt.service.ItemCatServiceImpl)")    //@Pointcut("within(com.jt.service.*)")   // .* 一级包门路   ..* 所有子孙后代包    //@Pointcut("execution(返回值类型 包名.类名.办法名(参数列表))")    @Pointcut("execution(* com.jt.service..*.*(..))")    //正文: 返回值类型任意类型   在com.jt.service下的所有子孙类   以add结尾的办法,任意参数类型    public void pointCut(){    }    /**     * 需要:     *      1.获取指标办法的门路     *      2.获取指标办法的参数.     *      3.获取指标办法的名称     */    @Before("pointCut()")    public void before(JoinPoint joinPoint){        String classNamePath = joinPoint.getSignature().getDeclaringTypeName();        String methodName = joinPoint.getSignature().getName();        Object[] args = joinPoint.getArgs();        System.out.println("办法门路:"+classNamePath);        System.out.println("办法参数:"+ Arrays.toString(args));        System.out.println("办法名称:"+methodName);    }    @Around("pointCut()")    public Object around(ProceedingJoinPoint joinPoint){        try {            System.out.println("盘绕告诉开始");            Object obj = joinPoint.proceed();            //如果有下一个告诉,就执行下一个告诉,如果没有就执行指标办法(业务办法)            System.out.println("盘绕告诉完结");            return null;        } catch (Throwable throwable) {            throwable.printStackTrace();            throw new RuntimeException(throwable);        }    }}` *   1*   2*   3*   4*   5*   6*   7*   8*   9*   10*   11*   12*   13*   14*   15*   16*   17*   18*   19*   20*   21*   22*   23*   24*   25*   26*   27*   28*   29*   30*   31*   32*   33*   34*   35*   36*   37*   38*   39*   40*   41*   42*   43*   44*   45*   46*   47*   48*   49*   50*   51*   52*   53*   54*   55*   56*   57*   58*   59*   60*   61*   62*   63*   64

5.4 AOP实现Redis缓存

5.4.1 业务实现策略

1).须要自定义注解CacheFind
2).设定注解的参数 key的前缀,数据的超时工夫.
3).在办法中标识注解.
4).利用AOP 拦挡指定的注解.
5).应该应用Around告诉实现缓存业务.

5.4.2 编辑自定义注解

`@Target(ElementType.METHOD)         //注解对办法无效@Retention(RetentionPolicy.RUNTIME) //运行期无效public @interface CacheFind {    public String preKey(); //定义key的前缀    public int seconds() default 0;   //定义数据的超时工夫.}` *   1*   2*   3*   4*   5*   6*   7*   8

5.4.3 办法中标识注解

5.4.4 编辑CacheAOP

package com.jt.aop;import com.jt.anno.CacheFind;import com.jt.util.ObjectMapperUtil;import lombok.extern.apachecommons.CommonsLog;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Service;import redis.clients.jedis.Jedis;import java.lang.reflect.Method;import java.util.Arrays;@Aspect //标识我是一个切面@Component  //交给Spring容器治理public class CacheAOP {    @Autowired    private Jedis jedis;    /**     * 注意事项:  当有多个参数时,joinPoint必须位于第一位.     * 需要:     *      1.筹备key= 注解的前缀 + 用户的参数     *      2.从redis中获取数据     *         有: 从缓存中获取数据之后,间接返回值     *         没有: 查询数据库之后再次保留到缓存中即可.     *     * 办法:     *      动静获取注解的类型,看上去是注解的名称,然而本质是注解的类型. 只有切入点表达式满足条件     *      则会传递注解对象类型.     * @param joinPoint     * @return     * @throws Throwable     */    @Around("@annotation(cacheFind)")    public Object around(ProceedingJoinPoint joinPoint,CacheFind cacheFind) throws Throwable {        Object result = null;   //定义返回值对象        String preKey = cacheFind.preKey();        String key = preKey + "::" + Arrays.toString(joinPoint.getArgs());        //1.校验redis中是否有数据        if(jedis.exists(key)){            //如果数据存在,须要从redis中获取json数据,之后间接返回            String json = jedis.get(key);            //1.获取办法对象,   2.获取办法的返回值类型            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();            //2.获取返回值类型            Class returnType = methodSignature.getReturnType();            result = ObjectMapperUtil.toObject(json,returnType);            System.out.println("AOP查问redis缓存!!!");        }else{            //代表没有数据,须要查询数据库            result = joinPoint.proceed();            //将数据转化为JSON            String json = ObjectMapperUtil.toJSON(result);            if(cacheFind.seconds() > 0){                jedis.setex(key, cacheFind.seconds(), json);            }else{                jedis.set(key,json);            }            System.out.println("AOP查询数据库!!!");        }        return result;    }   /* @Around("@annotation(com.jt.anno.CacheFind)")    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {        //1.获取指标对象的Class类型        Class targetClass = joinPoint.getTarget().getClass();        //2.获取指标办法名称        String methodName = joinPoint.getSignature().getName();        //3.获取参数类型        Object[] argsObj = joinPoint.getArgs();        Class[]  argsClass = null;        //4.对象转化为class类型        if(argsObj.length>0){           argsClass = new Class[argsObj.length];            for(int i=0;i<argsObj.length;i++){                argsClass[i] = argsObj[i].getClass();            }        }        //3.获取办法对象        Method targetMethod = targetClass.getMethod(methodName,argsClass);        //4.获取办法上的注解        if(targetMethod.isAnnotationPresent(CacheFind.class)){            CacheFind cacheFind = targetMethod.getAnnotation(CacheFind.class);            String key = cacheFind.preKey() + "::"                         +Arrays.toString(joinPoint.getArgs());            System.out.println(key);        }        Object object = joinPoint.proceed();        System.out.println("盘绕开始后");        return object;    }*/   /* //切面 = 切入点表达式 + 告诉办法    //@Pointcut("bean(itemCatServiceImpl)")    //@Pointcut("within(com.jt.service.ItemCatServiceImpl)")    //@Pointcut("within(com.jt.service.*)")   // .* 一级包门路   ..* 所有子孙后代包    //@Pointcut("execution(返回值类型 包名.类名.办法名(参数列表))")    @Pointcut("execution(* com.jt.service..*.*(..))")    //正文: 返回值类型任意类型   在com.jt.service下的所有子孙类   以add结尾的办法,任意参数类型    public void pointCut(){    }*/    /**     * 需要:     *      1.获取指标办法的门路     *      2.获取指标办法的参数.     *      3.获取指标办法的名称     */   /* @Before("pointCut()")    public void before(JoinPoint joinPoint){        String classNamePath = joinPoint.getSignature().getDeclaringTypeName();        String methodName = joinPoint.getSignature().getName();        Object[] args = joinPoint.getArgs();        System.out.println("办法门路:"+classNamePath);        System.out.println("办法参数:"+ Arrays.toString(args));        System.out.println("办法名称:"+methodName);    }    @Around("pointCut()")    public Object around(ProceedingJoinPoint joinPoint){        try {            System.out.println("盘绕告诉开始");            Object obj = joinPoint.proceed();            //如果有下一个告诉,就执行下一个告诉,如果没有就执行指标办法(业务办法)            System.out.println("盘绕告诉完结");            return null;        } catch (Throwable throwable) {            throwable.printStackTrace();            throw new RuntimeException(throwable);        }    }*/}