spring boot+ehcache简单存取。以token为例

1.导入pom坐标,spring boot内置了encache的版本`

        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>

2.配置ehcache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="http://www.ehcache.org/v3
        http://www.ehcache.org/schema/ehcache-core.xsd">
         <!-- 公共部分 -->
    <cache-template name="common">
        <key-type>java.lang.Object</key-type>
        <value-type>java.lang.Object</value-type>
        <resources>
            <heap>10000</heap>
        </resources>
    </cache-template>
    <!-- 存放token的缓存:只要7天内不访问,就失效 -->
    <cache alias="token" uses-template="common">
        <expiry>
            <tti unit="days">7</tti>
        </expiry>
    </cache>

    <!-- 默认缓存:永不过期 -->
    <cache alias="default" uses-template="common">
        <expiry>
            <none/>
        </expiry>
    </cache>
</config>

3.创建encache工具类

public class Caches {
    private static final CacheManager MGR;
    private static final Cache<Object, Object> DEFAULT_CACHE;
    private static final Cache<Object, Object> TOKEN_CACHE;
    static {
        // 初始化缓存管理器
        URL url = Caches.class.getClassLoader().getResource("ehcache.xml");
        assert url != null;
        Configuration cfg = new XmlConfiguration(url);
        MGR = CacheManagerBuilder.newCacheManager(cfg);
        MGR.init();

        // 缓存对象
        DEFAULT_CACHE = MGR.getCache("default", Object.class, Object.class);
        TOKEN_CACHE = MGR.getCache("token", Object.class, Object.class);
    }
	//封装
    public static void putToken(Object key, Object value) {
        if (key == null || value == null) return;
        TOKEN_CACHE.put(key, value);
    }

    public static void removeToken(Object key) {
        if (key == null) return;
        TOKEN_CACHE.remove(key);
    }
}

4.在impl中调用putToken

        String token= UUID.randomUUID().toString();
        Caches.putToken(token,vo);
        vo.setToken(token);
        return vo;

此时返回vo到客户端
运行 然后尝试将token存入到vo对象中结果报错,看上去是缺少相应类,于是将相应的pom坐标导入

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBElement
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

5.重新部署运行,在localStorage成功找到
在这里插入图片描述
每一次请求均有token,方便给后续做权限控制
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Ehcache 缓存来配置 Spring Boot 的步骤如下: 1. 添加 Ehcache 依赖:在 pom.xml 文件中添加 Ehcache 的相关依赖。 ```xml <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> ``` 2. 创建 Ehcache 配置文件:在 src/main/resources 目录下创建一个 ehcache.xml 文件,并配置缓存的名称、容量、过期时间等信息。 ```xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <cache alias="myCache"> <expiry> <ttl unit="seconds">60</ttl> </expiry> <heap unit="entries">100</heap> </cache> </config> ``` 3. 配置 Spring Boot 应用程序:在 Spring Boot 的配置类上使用 @EnableCaching 注解开启缓存功能,并指定 Ehcache缓存管理器。 ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { @Bean public EhCacheCacheManager cacheManager(CacheManager ehCacheManager) { return new EhCacheCacheManager(ehCacheManager); } } ``` 4. 使用缓存注解:在需要缓存的方法上使用 Spring缓存注解,比如 @Cacheable、@CachePut、@CacheEvict 等。 ```java @Service public class MyService { @Cacheable("myCache") public String getCachedData(String key) { // 从数据库或其他数据源中获取数据 return data; } } ``` 以上是使用 Ehcache 缓存配置 Spring Boot 的基本步骤。请根据你的具体需求进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值