• Cache缓存


    一般都是在凌晨写东西 比较乱 算是个记录 简单粗暴
    环境数据配置搭建
    依赖缓存

        <!--redis -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
                <version>RELEASE</version>
            </dependency>
      <!--redis end -->
    <!--SpringCache -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <!--SpringCache -->

    现在yml文件里面配置redis 方便缓存

    spring:
        datasource:
          redis:
           database: 0
            host: 127.0.0.1
            port: 6379
            password: 123456
            pool.max-idle: 8
            pool.min-idle: 0
            pool.max-active: 8
            pool.max-wait: 1
            timeout: 5000
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
     
    jpa:
        database: mysql
        show-sql: true
        hibernate:
          ddl-auto: update
          #####  jpa默认方式
          naming:
            implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
        properties:
          hibernate:
            dialect: org.hibernate.dialect.MySQL5Dialect
     

    1.springCache 注解申明式缓存
    1.1 开启注解
    //在启动类上申明 缓存注解启动
    @EnableCaching

    //判断当前方法缓存中是否有数据 没有则添加进缓存中
    @Cacheable

    @Cacheable(value = "house",key = "#house.id")
        @Override
        public House findOneHouse(House house) {
            House one = iHouseDao.findOne(house.getId());
    
            return one;
        }

    //将数据添加进缓存中去
    @CachePut

    @CachePut(value = "house",key = "#house.id")
        @Override
        public House saveHouse(House house) {
            House save = iHouseDao.save(house);
            return  save;
        }

    //删除缓存
    @CacheEvict

     @CacheEvict(value = "house")
        @Override
        public void deltedHouse(House house) {
            iHouseDao.delete(house);
        }

    //这个注解就是为了使用多个@CachePut, @CacheEvict或者是多个@Cacheable在同一个方法上,因为多个缓存中的条件表达式可能是不一样的.例子
    @Cacheing(evict={@CacheEvict("primary"), @Cacheable(value = "house",key = "#house.id")})

    模拟请求一个接口 提前写好的

    第一次请求

     有sql查询
    第二次

     缓存成功
    打开redis 发现redis中保存了一条数据

    其他几个就先不弄了 打瞌睡了

    注解的方式虽然方便 但是不能指定缓存过期时间
    2018年9月27日00:03:11 继续
    Ehcache
    使用时 添加jar包

    <!--Ehcache start -->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </dependency>
            <!-- Ehcache end-->

    application.yml中指定cache Type

    #缓存配置  type如果切换成redis 将自动向redis 设置key值 则不需要config
      cache:
        type: ehcache
        jcache:
          config: ehcache.xml

    加入Ehcache文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <ehcache>
        <!-- 缓存名称   与Cacheable name 对应 -->
        <cache name="house"
               maxElementsInMemory="1000"
               eternal="false"
               maxElementsLocalHeap="0"
               timeToIdleSeconds="200"
        ></cache>
    
    
        <!-- eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false -->
        <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
        <!-- timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,
        如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。
        只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态 -->
    </ehcache>
  • 相关阅读:
    ionic3开发环境搭建与配置(win10系统)
    angular4打包以后,刷新报404
    css3文字渐变无效果的解决方案
    node-sass安装失败的解决方案
    python logging 重复写日志问题
    进程和线程的概念
    软件开发目录规范
    相对导入
    python引入导入自定义模块和外部文件
    异常处理
  • 原文地址:https://www.cnblogs.com/liaohongbin/p/9888903.html
Copyright © 2020-2023  润新知