十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
org.springframework.boot spring-boot-starter-cache net.sf.ehcache ehcache
创建ehcache.xml配置文件
修改springboot配置文件,引入ehcache.xml配置文件spring:
cache:
type: ehcache
ehcache:
config: classpath:ehcache/ehcache.xml
启用@EnableCaching
注解@SpringBootApplication
@EnableCaching
public class Application{public static void main(String[] args) {SpringApplication.run(Application.class, args);
}
}
实体类实现可序列化接口Serializable由于需要实体类支持缓存中的磁盘存储,所以需要实体类实现可序列化接口。
public class User implements Serializable{...
}
添加缓存注解@Cacheable
、@CacheEvict
@Cacheable
缓存数据@Service
public class UserServiceImpl implements UserService {@Autowired
private UserMapper userMapper;
@Override
@Cacheable(value="users")
public User selectUserById(int id) {User user=this.userMapper.selectUserById(id);
System.out.println("load data from db");
return user;
}
}
@CacheEvict
清除缓存@Service
public class UserServiceImpl implements UserService {@Autowired
private UserMapper userMapper;
@Override
@Cacheable(value="users")
public User selectUserById(int id) {User user=this.userMapper.selectUserById(id);
System.out.println("load data from db");
return user;
}
@CacheEvict(value="users", allEntries=true)
public void saveUsers(Users users) {this.userMapper.save(users);
}
@CacheEvict(value="users", allEntries=true)
public void deleteUserById(int id) {this.userMapper.deleteUserById(id);
}
}
其它
设置java.io.tmpdir
子目录
参考https://www.cnblogs.com/xzmiyx/p/9897623.html
https://blog.csdn.net/qq_33285292/article/details/108152912
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧