├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── jarvis
│ │ └── cache_example
│ │ ├── App.java
│ │ ├── common
│ │ ├── dao
│ │ │ └── UserDAO.java
│ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ ├── service
│ │ │ ├── UserService.java
│ │ │ └── UserServiceImpl.java
│ │ └── to
│ │ │ └── UserTO.java
│ │ └── ui
│ │ └── web
│ │ └── IndexController.java
├── resources
│ ├── applicationContext.xml
│ ├── datasource-config.xml
│ ├── log4j.xml
│ ├── module.properties
│ ├── mybatis-config.xml
│ └── mybatis
│ │ └── UserMapper.xml
└── webapp
│ ├── WEB-INF
│ ├── dispatcher-servlet.xml
│ └── web.xml
│ ├── index.html
│ └── index.jsp
└── test
└── java
└── com
└── jarvis
└── cache_example
└── WaitTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | .settings
3 | .classpath
4 | .project
5 | .idea
6 | *.iml
7 | *.class
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # cache-example
2 | ---------------------------------------------
3 |
4 | [自动加载缓存代码](https://github.com/qiujiayu/AutoLoadCache)
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | com.github.qiujiayu
4 | cache-example
5 | 0.0.1-SNAPSHOT
6 | war
7 | cache-example
8 | http://maven.apache.org
9 |
10 | UTF-8
11 | 3.2.10.RELEASE
12 | 6.6
13 |
14 |
15 | cache-example
16 |
17 |
18 | maven-compiler-plugin
19 | 3.1
20 |
21 | 1.7
22 | 1.7
23 |
24 |
25 |
26 |
27 |
28 |
29 | javax.servlet
30 | servlet-api
31 | 2.5
32 | provided
33 |
34 |
35 | javax.servlet
36 | jstl
37 | 1.2
38 | runtime
39 |
40 |
41 | com.github.qiujiayu
42 | autoload-cache
43 | ${autoload-cache.version}
44 |
45 |
46 | org.slf4j
47 | slf4j-log4j12
48 | 1.7.24
49 |
50 |
51 | log4j
52 | log4j
53 | 1.2.17
54 |
55 |
56 | redis.clients
57 | jedis
58 | 2.8.1
59 |
60 |
61 | com.caucho
62 | hessian
63 | 4.0.38
64 |
65 |
66 | org.springframework
67 | spring-context
68 | ${spring.version}
69 |
70 |
71 | org.springframework
72 | spring-core
73 | ${spring.version}
74 |
75 |
76 | org.springframework
77 | spring-beans
78 | ${spring.version}
79 |
80 |
81 | org.springframework
82 | spring-web
83 | ${spring.version}
84 |
85 |
86 | org.springframework
87 | spring-webmvc
88 | ${spring.version}
89 |
90 |
91 | org.springframework
92 | spring-aop
93 | ${spring.version}
94 |
95 |
96 | org.springframework
97 | spring-aspects
98 | ${spring.version}
99 |
100 |
101 | org.springframework
102 | spring-expression
103 | ${spring.version}
104 |
105 |
106 | org.springframework
107 | spring-jdbc
108 | ${spring.version}
109 |
110 |
111 | org.springframework
112 | spring-orm
113 | ${spring.version}
114 |
115 |
116 | org.springframework
117 | spring-tx
118 | ${spring.version}
119 |
120 |
121 | org.aspectj
122 | aspectjrt
123 | 1.8.2
124 |
125 |
126 | org.aspectj
127 | aspectjweaver
128 | 1.8.2
129 |
130 |
131 | org.mybatis
132 | mybatis
133 | 3.3.1
134 |
135 |
136 |
137 | ognl
138 | ognl
139 | 3.1.2
140 |
141 |
142 | javassist
143 | javassist
144 |
145 |
146 |
147 |
148 | org.javassist
149 | javassist
150 | 3.20.0-GA
151 |
152 |
153 | cglib
154 | cglib
155 | 3.2.0
156 |
157 |
158 | org.mybatis
159 | mybatis-spring
160 | 1.2.4
161 |
162 |
163 |
164 | mysql
165 | mysql-connector-java
166 | 5.1.35
167 |
168 |
169 | com.cloudhopper.proxool
170 | proxool
171 | 0.9.1
172 |
173 |
174 | commons-logging
175 | commons-logging
176 |
177 |
178 |
179 |
180 | org.apache.commons
181 | commons-compress
182 | 1.11
183 |
184 |
185 | com.alibaba
186 | fastjson
187 | 1.2.12
188 |
189 |
190 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/App.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example;
2 |
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 |
5 | import com.jarvis.cache_example.common.dao.UserDAO;
6 | import com.jarvis.cache_example.common.to.UserTO;
7 |
8 | /**
9 | * Hello world!
10 | */
11 | public class App {
12 |
13 | private static ClassPathXmlApplicationContext context;
14 |
15 | private static UserDAO userDAO;
16 |
17 | /**
18 | * 初始化
19 | */
20 | private static void init() {
21 | context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "datasource-config.xml"});
22 | userDAO=(UserDAO)context.getBean("userDAO");
23 | }
24 |
25 | public static void main(String[] args) throws Exception {
26 | init();
27 | System.out.println("Hello World!");
28 |
29 | test2();
30 |
31 | }
32 |
33 | public static void test3() {
34 | String userName="testUser";
35 | UserTO user=userDAO.addUser(userName);
36 | System.out.println(user.getId());
37 | user=userDAO.getUserById(user.getId());
38 | System.out.println(user.getId());
39 | }
40 |
41 | public static void test1() {
42 | Thread thread=new Thread(new Runnable() {
43 |
44 | @Override
45 | public void run() {
46 | int cnt=0;
47 | while(true) {
48 | loadData();
49 | try {
50 | Thread.sleep(1000);
51 | } catch(InterruptedException e) {
52 | // TODO Auto-generated catch block
53 | e.printStackTrace();
54 | }
55 | cnt++;
56 | if(cnt > 5) {
57 | userDAO.clearUserCache();
58 | loadData();
59 | break;
60 | }
61 | }
62 | }
63 |
64 | private void loadData() {
65 | for(int i=1; i < 20; i++) {
66 | UserTO to=userDAO.getUserById(i);
67 | System.out.println(to.getName());
68 | }
69 | }
70 |
71 | });
72 | thread.start();
73 | }
74 |
75 | public static void test2() throws Exception {
76 | userDAO.getUserById2(100);
77 | UserTO user=new UserTO();
78 | user.setId(100);
79 | user.setName("name100");
80 | userDAO.getUserById2(100);
81 | userDAO.getUserById2(100);
82 | userDAO.getUserById2(100);
83 | userDAO.getUserById2(100);
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/common/dao/UserDAO.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.common.dao;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Random;
6 |
7 | import com.jarvis.cache.annotation.Cache;
8 | import com.jarvis.cache.annotation.CacheDelete;
9 | import com.jarvis.cache.annotation.CacheDeleteKey;
10 | import com.jarvis.cache.type.CacheOpType;
11 | import com.jarvis.cache_example.common.to.UserTO;
12 |
13 | public class UserDAO {
14 |
15 | private static final String cacheName="user";
16 |
17 | private static final int expire=121;
18 |
19 | /**
20 | * 添加用户的同时,把数据放到缓存中
21 | * @param userName
22 | * @return
23 | */
24 | @Cache(expire=expire, key="'" + cacheName + "'+#retVal.id", opType=CacheOpType.WRITE)
25 | public UserTO addUser(String userName) {
26 | UserTO user=new UserTO();
27 | user.setName(userName);
28 | Random rand=new Random();
29 | // 数据库返回ID
30 | Integer id=rand.nextInt(100000);
31 | user.setId(id);
32 | System.out.println("add User:" + id);
33 |
34 | return getUserById(id);
35 | }
36 |
37 | /**
38 | * 使用 hash 方法,将参数转为字符串
39 | * @param user
40 | * @return
41 | */
42 | @Cache(expire=expire, key="'" + cacheName + "'+#hash(#args)")
43 | public List getUserList(UserTO user) {
44 | List list=new ArrayList();
45 | list.add(user);
46 | System.out.println("getUserList from dao");
47 | return list;
48 | }
49 |
50 | /**
51 | * 使用自定义缓存Key,并在指定的条件下才进行缓存。
52 | * @param id
53 | * @return
54 | */
55 | @Cache(expire=expire, autoload=true, key="'user_dao_getUserById'+#args[0]", condition="#args[0]>0")
56 | public UserTO getUserById(Integer id) {
57 | UserTO user=new UserTO();
58 | user.setId(id);
59 | user.setName("name" + id);
60 | System.out.println("getUserById from dao");
61 | return user;
62 | }
63 |
64 | /**
65 | * 使用自定义缓存Key,并在指定的条件下才进行缓存。
66 | * @param id
67 | * @return
68 | */
69 | @Cache(expire=expire, autoload=false, key="'user_dao_getUserById2'+#args[0]", condition="#args[0]>0")
70 | public UserTO getUserById2(Integer id) throws Exception {
71 | Thread thread=Thread.currentThread();
72 | System.out.println("thread:" + thread.getName() + ";getUserById2");
73 | try {
74 | // 模拟阻塞
75 | Thread.sleep(100);
76 | } catch(InterruptedException e) {
77 | e.printStackTrace();
78 | }
79 | UserTO user=new UserTO();
80 | user.setId(id);
81 | user.setName("name" + id);
82 | // throw new Exception("test");// 异常情况测试
83 | return user;
84 | }
85 |
86 | // 注意:因为没有用 SpEL表达式,所以不需要用单引号
87 | @CacheDelete({@CacheDeleteKey(value="'user_dao_getUserById2'+#args[0]", condition="#args[0]>0")})
88 | public void clearUserById2Cache(Integer id) {
89 | System.out.println("clearUserById2Cache");
90 | // save to db
91 | }
92 |
93 | @CacheDelete({@CacheDeleteKey(value="'user'+#args[0].id", condition="null != #args[0]")})
94 | public void updateUserName(UserTO user) {
95 | if(null != user) {
96 | System.out.println("update user name:" + user.getName());
97 | } else {
98 | System.out.println("use is null");
99 | }
100 | // save to db
101 | }
102 |
103 | // 注意:因为没有用 SpEL表达式,所以不需要用单引号
104 | @CacheDelete({@CacheDeleteKey(value="user*")})
105 | public void clearUserCache() {
106 | System.out.println("clearUserCache");
107 | // save to db
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/common/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.common.mapper;
2 |
3 | import com.jarvis.cache.annotation.Cache;
4 | import com.jarvis.cache.annotation.CacheDelete;
5 | import com.jarvis.cache.annotation.CacheDeleteKey;
6 | import com.jarvis.cache_example.common.to.UserTO;
7 |
8 | public interface UserMapper {
9 |
10 | @Cache(expire=600, autoload=true, key="'user_mapper_getUserById_'+#args[0]", condition="#args[0]>0")
11 | UserTO getUserById(Integer id);
12 |
13 | int addUser(UserTO user);
14 |
15 | @CacheDelete({@CacheDeleteKey(value="'user_mapper_getUserById_'+#args[0].id")})
16 | int incAge(UserTO user);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/common/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.common.service;
2 |
3 | import java.util.List;
4 |
5 | import com.jarvis.cache_example.common.to.UserTO;
6 |
7 | public interface UserService {
8 |
9 | UserTO getUserById(Integer id);
10 |
11 | UserTO getUserById2(Integer id);
12 |
13 | List getUserList();
14 |
15 | void updateUser(UserTO user);
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/common/service/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.common.service;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | import com.jarvis.cache_example.common.dao.UserDAO;
9 | import com.jarvis.cache_example.common.mapper.UserMapper;
10 | import com.jarvis.cache_example.common.to.UserTO;
11 |
12 | @Service
13 | public class UserServiceImpl implements UserService {
14 |
15 | @Autowired
16 | private UserDAO userDAO;
17 |
18 | @Autowired
19 | private UserMapper userMapper;
20 |
21 | @Override
22 | public UserTO getUserById(Integer id) {
23 | return userMapper.getUserById(id);
24 | }
25 |
26 | @Override
27 | public UserTO getUserById2(Integer id) {
28 | return userDAO.getUserById(id);
29 | }
30 |
31 | @Override
32 | public void updateUser(UserTO user) {
33 | userDAO.updateUserName(user);
34 | }
35 |
36 | @Override
37 | public List getUserList() {
38 | return userDAO.getUserList(new UserTO());
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/common/to/UserTO.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.common.to;
2 |
3 | import java.io.Serializable;
4 |
5 | public class UserTO implements Serializable {
6 |
7 | private static final long serialVersionUID=-2510806368101425770L;
8 |
9 | private Integer id;
10 |
11 | private String name;
12 |
13 | private Integer age;
14 |
15 | public Integer getId() {
16 | return id;
17 | }
18 |
19 | public void setId(Integer id) {
20 | this.id=id;
21 | }
22 |
23 | public String getName() {
24 | return name;
25 | }
26 |
27 | public void setName(String name) {
28 | this.name=name;
29 | }
30 |
31 | public Integer getAge() {
32 | return age;
33 | }
34 |
35 | public void setAge(Integer age) {
36 | this.age=age;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "UserTO [id=" + id + ", name=" + name + ", age=" + age + "]";
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/jarvis/cache_example/ui/web/IndexController.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example.ui.web;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Controller;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.servlet.ModelAndView;
10 |
11 | import com.jarvis.cache_example.common.service.UserService;
12 | import com.jarvis.cache_example.common.to.UserTO;
13 |
14 | @Controller
15 | public class IndexController {
16 |
17 | @Autowired
18 | private UserService userService;
19 |
20 | @RequestMapping("/index.html")
21 | public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
22 | int id=1;
23 | UserTO user=userService.getUserById(id);
24 | request.setAttribute("user", user);
25 | UserTO user2=userService.getUserById2(id);
26 | request.setAttribute("user", user);
27 | request.setAttribute("user2", user2);
28 | return new ModelAndView("/index");
29 | }
30 |
31 | @RequestMapping("/updateuser.html")
32 | public ModelAndView updateUser(HttpServletRequest request, HttpServletResponse response) {
33 | int id=100;
34 | UserTO user=userService.getUserById(id);
35 | userService.updateUser(user);
36 | return index(request, response);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
12 |
13 |
14 | classpath:module.properties
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
62 |
63 |
64 |
65 |
66 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
--------------------------------------------------------------------------------
/src/main/resources/datasource-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/src/main/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/src/main/resources/module.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://192.168.2.150:3306/cache_example?characterEncoding=utf8
3 | jdbc.username=test
4 | jdbc.password=123456
5 | jdbc.initConnections=5
6 | jdbc.poolSize=20
7 |
8 | # Global Settings.
9 | redis1.host=192.168.2.150
10 | redis1.port=6379
11 |
12 | redis2.host=192.168.2.150
13 | redis2.port=6380
14 |
15 | redis3.host=192.168.2.150
16 | redis3.port=6381
--------------------------------------------------------------------------------
/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/main/resources/mybatis/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | select ID, NAME
10 |
11 |
12 |
17 |
18 |
19 | insert into USER (NAME, AGE)
20 | values(#{name}, #{age})
21 |
22 |
23 | update USER AGE = AGE + 1 where ID = #{id}
24 |
25 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/dispatcher-servlet.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | webAppRootKey
7 | cache_example.root
8 |
9 |
10 | log4jConfigLocation
11 | classpath:log4j.xml
12 |
13 |
14 | log4jRefreshInterval
15 | 60000
16 |
17 |
18 | contextConfigLocation
19 | classpath:applicationContext.xml;classpath:datasource-config.xml
20 |
21 |
22 |
23 | org.springframework.web.context.ContextLoaderListener
24 |
25 |
26 | org.springframework.web.util.IntrospectorCleanupListener
27 |
28 |
29 | org.springframework.web.util.Log4jConfigListener
30 |
31 |
32 | 编码设置
33 | ecodingFilter
34 | org.springframework.web.filter.CharacterEncodingFilter
35 |
36 | encoding
37 | UTF-8
38 |
39 |
40 | forceEncoding
41 | true
42 |
43 |
44 |
45 | ecodingFilter
46 | /*
47 |
48 |
49 | dispatcher
50 | org.springframework.web.servlet.DispatcherServlet
51 |
52 |
53 | dispatcher
54 | *.html
55 |
56 |
57 | index.html
58 |
59 |
--------------------------------------------------------------------------------
/src/main/webapp/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | index.html
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/main/webapp/index.jsp:
--------------------------------------------------------------------------------
1 | <%@ page contentType="text/html; charset=UTF-8" trimDirectiveWhitespaces="true"%>
2 |
3 |
4 |
5 |
6 |
7 | cache example index
8 |
9 |
10 |
11 |
12 |
13 | userName:${user.name}
14 |
15 | userName2:${user2.name}
16 |
17 | 删除缓存
18 |
19 | 缓存管理后台
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/test/java/com/jarvis/cache_example/WaitTest.java:
--------------------------------------------------------------------------------
1 | package com.jarvis.cache_example;
2 |
3 | import java.util.List;
4 | import java.util.concurrent.CountDownLatch;
5 |
6 | import org.springframework.context.ApplicationContext;
7 | import org.springframework.context.support.ClassPathXmlApplicationContext;
8 |
9 | import com.alibaba.fastjson.JSON;
10 | import com.jarvis.cache_example.common.dao.UserDAO;
11 | import com.jarvis.cache_example.common.to.UserTO;
12 |
13 | public class WaitTest {
14 |
15 | private static ApplicationContext applicationContext=null;
16 |
17 | private static UserDAO userDAO;
18 |
19 | public static void main(String[] args) {
20 | String[] tmp=new String[]{"applicationContext.xml", "datasource-config.xml"};
21 | applicationContext=new ClassPathXmlApplicationContext(tmp);
22 | userDAO=applicationContext.getBean(UserDAO.class);
23 | // countDownTest();
24 | test();
25 | test();
26 | try {
27 | Thread.sleep(80 * 1000);// 看看异常刷新是否生效
28 | } catch(InterruptedException e) {
29 | // TODO Auto-generated catch block
30 | e.printStackTrace();
31 | }
32 | test();
33 | try {
34 | Thread.sleep(120 * 1000);
35 | } catch(InterruptedException e) {
36 | // TODO Auto-generated catch block
37 | e.printStackTrace();
38 | }
39 | }
40 |
41 | private static void test() {
42 | UserTO data=new UserTO();
43 | data.setId(1);
44 | data.setAge(20);
45 | data.setName("test1");
46 | List res1=userDAO.getUserList(data);
47 | System.out.println("res1==" + JSON.toJSONString(res1));
48 | data.setAge(30);
49 | data.setName("test2");
50 | List res2=userDAO.getUserList(data);
51 | System.out.println("res2==" + JSON.toJSONString(res2));
52 | }
53 |
54 | private static void countDownTest() {
55 | int threadCnt=100;
56 | userDAO.clearUserById2Cache(100);
57 | final CountDownLatch count=new CountDownLatch(threadCnt);
58 | for(int i=0; i < threadCnt; i++) {
59 | Thread thread=new Thread(new Runnable() {
60 |
61 | @Override
62 | public void run() {
63 | UserTO user=null;
64 | try {
65 | user=userDAO.getUserById2(100);
66 | Thread thread=Thread.currentThread();
67 | System.out.println(thread.getName() + " finished " + user.getName());
68 | } catch(Exception e) {
69 | e.printStackTrace();
70 | }
71 |
72 | count.countDown();
73 | }
74 | }, "thread" + i);
75 | thread.start();
76 | }
77 | try {
78 | count.await();
79 | } catch(InterruptedException e) {
80 | e.printStackTrace();
81 | }
82 | userDAO.clearUserById2Cache(100);
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------