package com.ehcache.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ehcache.entity.User;
import com.ehcache.factory.CacheManagerFactory;
import com.ehcache.factory.UserFactory;
import com.ehcache.service.UserService;
import com.google.gson.Gson;
import net.sf.ehcache.Element;
@RestController
@RequestMapping("/o")
public class OperationController {
@Autowired
private UserService userService;
Gson gson = new Gson();
CacheManagerFactory cmf = CacheManagerFactory.getInstance();
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(HttpServletRequest request){
// 保存一个新用户
String uid = userService.save(UserFactory.createUser());
User user = userService.getUserById(uid);
user.setUsername("xiaoli");
userService.update(user);
// 查询该用户
System.out.println(gson.toJson(user, User.class));
/*System.out.println();
// 再查询该用户
User user = userService.getUserById(uid);
System.out.println(gson.toJson(user, User.class));
System.out.println();
// 更新该用户
userService.update(user);
// 更新好了再查询该用户
System.out.println(gson.toJson(userService.getUserById(uid), User.class));
System.out.println();
// 删除该用户
userService.del(uid);
System.out.println();
// 删除好了再查询该用户
System.out.println(gson.toJson(userService.getUserById(uid), User.class));*/
// 再保存一个新用户
// String uid1 = userService.save(UserFactory.createUser());
return uid;
}
@RequestMapping(value = "/test1", method = RequestMethod.GET)
public String test1(HttpServletRequest request,String key){
String res = "";
Element element = cmf.getElement("userCache", "map");
if(element == null){
Map<String, String> map = new HashMap<String, String>();
map.put(key, key);
cmf.setElement("userCache", new Element("map", map));
}else{
Map<String, String> map = (Map<String, String>) element.getValue();
res = map.get(key);
if(res == null){
map.put(key, key);
// 多次测试发现,存在同名Element是,重复put的是无法复制的,因此当遇到两个节点同步不上的时候,先remove后put。
cmf.getCache("userCache").remove("map");
cmf.setElement("userCache", new Element("map", map));
res = "0;null";
}
}
return res;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot+mybatis+ehcache实现缓存数据
共46个文件
xml:9个
class:8个
java:8个
5星 · 超过95%的资源 需积分: 50 54 下载量 197 浏览量
2017-11-26
19:40:06
上传
评论 2
收藏 22.6MB RAR 举报
温馨提示
ehcache是一个快速内存缓存框架,java项目里用起来很方便,本来想实现分布式缓存的,但是实践下来redis更适合分布式,所以这里面把分布缓存的配置注释掉了
资源推荐
资源详情
资源评论
收起资源包目录
ehcache-test.rar (46个子文件)
ehcache-test
pom.xml 2KB
target
test-classes
com
ehcache
EhcacheTestApplicationTests.class 633B
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 628B
createdFiles.lst 0B
testCompile
default-testCompile
inputFiles.lst 117B
createdFiles.lst 0B
generated-test-sources
test-annotations
ehcache-test-0.0.1-SNAPSHOT.jar.original 11KB
classes
META-INF
maven
com.ehcache
ehcache-test
pom.xml 2KB
pom.properties 257B
MANIFEST.MF 342B
application.properties 569B
com
ehcache
dao
UserDao.class 2KB
factory
UserFactory.class 2KB
CacheManagerFactory.class 2KB
EhcacheTestApplication.class 956B
controller
OperationController.class 3KB
service
UserService.class 2KB
entity
User.class 910B
ehcache.xml 3KB
mybatis
mybatis-config.xml 956B
mapper
userMapper.xml 1000B
maven-archiver
pom.properties 133B
generated-sources
annotations
ehcache-test-0.0.1-SNAPSHOT.jar 25.93MB
mvnw 6KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.core.resources.prefs 214B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
ehcache
EhcacheTestApplicationTests.java 333B
main
resources
application.properties 569B
ehcache.xml 3KB
mybatis
mybatis-config.xml 956B
mapper
userMapper.xml 1000B
java
com
ehcache
dao
UserDao.java 1KB
factory
CacheManagerFactory.java 2KB
UserFactory.java 1KB
EhcacheTestApplication.java 726B
controller
OperationController.java 3KB
service
UserService.java 1KB
entity
User.java 503B
.project 978B
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
共 46 条
- 1
资源评论
- yy60602018-01-14谢谢,可用!
- franky882018-03-29谢谢,可用!
- phpxiangxue1002018-03-27非常好的资源
沙沙沙
- 粉丝: 1
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功