zoukankan      html  css  js  c++  java
  • Spring boot caching example

    spring cache API

    1. Types of cache

    1.1 In-memory caching 如 redis。

    1.2 database caching 如 hibernate cache。

    2. Spring boot cache annotations

    initialize your project https://start.spring.io/

    2.1 Create HTTP GET REST API

    Student.java

    package com.example.springcache.domain;
     
    public class Student {
     
        String id;
        String name;
        String clz;
     
        public Student(String id, String name, String clz) {
            super();
            this.id = id;
            this.name = name;
            this.clz = clz;
        }
     
        //Setters and getters //注意把set和get方法加上
    }
    

     StudentService.java

    package com.example.springcache.service;
     
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    import com.example.springcache.domain.Student;
     
    @Service
    public class StudentService
    {
        @Cacheable("student")
        public Student getStudentByID(String id)
        {
            try
            {
                System.out.println("Going to sleep for 5 Secs.. to simulate backend call.");
                Thread.sleep(1000*5);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
     
            return new Student(id,"Sajal" ,"V");
        }
    }
    

     StudentController.java

    package com.example.springcache.controller;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    import com.example.springcache.domain.Student;
    import com.example.springcache.service.StudentService;
     
    @RestController
    public class StudentController
    {
     
        @Autowired
        StudentService studentService;
     
        @GetMapping("/student/{id}")
        public Student findStudentById(@PathVariable String id)
        {
            System.out.println("Searching by ID  : " + id);
     
            return studentService.getStudentByID(id);
        }
    }
    

     Note:

    • service层方法用@Cacheable("student"),用于cache学生信息。

    2.2 Enable Spring managed Caching

    SpringCacheApplication.java

    package com.example.springcache;
     
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cache.annotation.EnableCaching;
     
    @SpringBootApplication
    @EnableCaching
    public class SpringCacheApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(SpringCacheApplication.class, args);
        }
    }
    

     2.3 Demo test

    http://localhost:8080/student/1
    

     通过变换id,http://localhost:8080/student/2 感受一下缓存的效果。

  • 相关阅读:
    【作业】Python
    【作业】判断某个数是否是素数,返回结果
    【案例】Python之列表反转
    Python模块
    【作业】Python-数据转换:将列表["mo","deng","ge"]和[1,2,3] 转换成[("mo",1),("deng",2),("ge",3)]
    【个人笔记】Python-zip()函数
    【作业】Python-数据转换:将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1
    【作业】Python-将元组(1,2,3) 和集合{"four",5,6}合成一个列表
    【作业】Python-函数封装:交换两个变量的值
    【个人笔记】Python-sorted()函数
  • 原文地址:https://www.cnblogs.com/chenqr/p/11144882.html
Copyright © 2011-2022 走看看