通用缓存SpringCache

概述

在项目中,我们通常会把高频的查询进行缓存。如资讯网站首页的文章列表、电商网站首页的商品列表、微博等社交媒体热搜的文章等等,当大量的用户发起查询时,借助缓存提高查询效率,同时减轻数据库压力。
目前的缓存框架有很多:比如Redis、Memcached、Guava、Caffeine等等

介绍


Spring Cache是Spring提供的通用缓存框架。它利用了AOP,实现了基于注解的缓存功能,使开发者不用关心底层使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了。用户使用Spring Cache,可以快速开发一个很不错的缓存功能。

入门案例

1.依赖

 <!--spring cache依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

2.开启缓存

package com.itheima.cache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching//开启缓存
public class CachingApplication {

	public static void main(String[] args) {
		SpringApplication.run(CachingApplication.class, args);
	}

}

3.配置注解

@Cacheable("user")
public User findById(Long id) {
 return userDao.findById(id);
}

注:通过这三步就基本实现操作缓存了

完整测试代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>spring-cache-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
    </parent>

    <dependencies>

        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--spring cache依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

    </dependencies>

</project>

CachingApplication启动类

package com.itheima.cache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class CachingApplication {

	public static void main(String[] args) {
		SpringApplication.run(CachingApplication.class, args);
	}

}

实体类

package com.itheima.cache.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private Long id;
    private String username;
}

UserDao

package com.itheima.cache.dao;

import com.itheima.cache.domain.User;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

/**
 * 模拟数据库
 */
@Repository
public class UserDao {

    public User findById(Long id){
        System.out.println("查询数据库");
        try {
            Thread.sleep(2000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new User(id,"张三");
    }

    public List<User> findAll() {
        List<User> list = Collections.EMPTY_LIST;
        list.add(new User(1l,"张三"));
        list.add(new User(2l,"李四"));
        list.add(new User(3l,"王五"));
        return list;
    }

    public void update(Long id) {
        System.out.println("根据id更新");
    }
}

UserService

package com.itheima.cache.service;

import com.itheima.cache.dao.UserDao;
import com.itheima.cache.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

   
    public User findById(Long id) {
        return userDao.findById(id);
    }

  
    public void update(Long id) {
        userDao.update(id);
    }
}

UserServiceTest

package com.itheima.cache.test;

import com.itheima.cache.domain.User;
import com.itheima.cache.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;


    /**
     * 根据id查询用户
     */
    @Test
    public void testFindById() {
        for (int i = 0; i < 5; i++) {
            User user = userService.findById(1l);
            System.out.println(user);
        }
    }

}

测试结果

这是没有加通用缓存的结果,都是从数据库中查询出来的

在UserService里面给他加注解缓存

package com.itheima.cache.service;

import com.itheima.cache.dao.UserDao;
import com.itheima.cache.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Cacheable(value="user")//指定key为user
    public User findById(Long id) {
        return userDao.findById(id);
    }

  
    public void update(Long id) {
        userDao.update(id);
    }
}

可以看见第一个从数据库中查询出来存到缓存中后,后面的都是从缓存中查询出来的

我们用了SpringCache做缓存后,我们如何将它用其他缓存框架来更换SpringCache呢?

默认情况下,SpringCache使用concurrentHashMap作为本地缓存存储数据。如果要使用其它的缓存框架,我们只需要做简单的配置即可。
 

所有意思就是如果我们之前使用的是SpringCache,那么我们就很容易的用其他缓存框架来代替

用Redis来替换SpringCache

做简单配置即可:

依赖

 <!--SpringDataRedis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

application.yml

spring:
  redis:
    port: 6379
    host: localhost

就这样后进行测试:

成功

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值