Skip to content

Commit d4c5685

Browse files
author
Frank
committed
studying ssm
1 parent b1bb048 commit d4c5685

6 files changed

Lines changed: 120 additions & 24 deletions

File tree

simple-spring-memcached/src/main/java/sample/ProductSampleRun.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,61 @@
11
package sample;
22

3+
import net.rubyeye.xmemcached.MemcachedClient;
4+
import net.rubyeye.xmemcached.MemcachedClientBuilder;
5+
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
6+
import net.rubyeye.xmemcached.exception.MemcachedException;
7+
import net.rubyeye.xmemcached.utils.AddrUtil;
38
import org.springframework.context.ApplicationContext;
49
import org.springframework.context.support.ClassPathXmlApplicationContext;
510
import sample.di.business.domain.Product;
611
import sample.di.business.service.ProductService;
12+
import sample.di.util.MemcachedUtil;
13+
14+
import java.io.IOException;
15+
import java.util.concurrent.TimeoutException;
16+
import java.util.function.Consumer;
717

818
public class ProductSampleRun {
19+
private static Consumer<ProductService> execReadThroughSingleCache = (ProductService productService) -> {
20+
String productName = "hoge";
21+
productService.addProduct(new Product(productName, 100));
22+
productService.findProduct(productName);
23+
productService.findProduct(productName);
24+
};
25+
26+
// private static Consumer<ProductService> execUpdateThroughSingleCache = (ProductService productService) -> {
27+
// String productName = "hoge";
28+
// productService.addProduct(new Product(productName, 100));
29+
// productService.findProduct(productName);
30+
// productService.findProduct(productName);
31+
// };
932

1033
public static void main(String[] args) {
34+
MemcachedClient memcachedClient = MemcachedUtil.getMemcachedClient("localhost:11211");
1135
ProductSampleRun productSampleRun = new ProductSampleRun();
12-
productSampleRun.execute();
36+
ProductService productService = productSampleRun.initProductService();
37+
38+
productSampleRun.executeCacheOperation(productService, memcachedClient, execReadThroughSingleCache);
1339
}
1440

15-
public void execute() {
16-
ProductService productService = initProductService();
1741

18-
String productName = "hoge";
19-
productService.addProduct(new Product(productName, 100));
20-
productService.findProduct(productName);
21-
productService.findProduct(productName);
22-
// productService.findProduct(productName);
42+
private void executeCacheOperation(ProductService productService, MemcachedClient memcachedClient, Consumer<ProductService> c){
2343

24-
// productService.getPrice(productName);
25-
// productService.findProduct(productName);
44+
System.out.println("-------- START -------");
45+
try {
46+
memcachedClient.flushAll();
47+
} catch (TimeoutException e) {
48+
e.printStackTrace();
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
} catch (MemcachedException e) {
52+
e.printStackTrace();
53+
}
2654

27-
// productService.addProduct(new Product(productName, 200));
28-
// productService.findProduct(productName);
29-
// productService.findProduct(productName);
30-
// productService.findProduct(productName);
55+
c.accept(productService);
56+
57+
System.out.println("-------- END -------");
58+
System.out.println();
3159
}
3260

3361
private ProductService initProductService() {

simple-spring-memcached/src/main/java/sample/di/business/domain/Product.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Product(String name, int price) {
1313
this.price = price;
1414
}
1515

16-
@CacheKeyMethod
16+
// @CacheKeyMethod
1717
public String getName() {
1818
return name;
1919
}

simple-spring-memcached/src/main/java/sample/di/dataaccess/ProductDaoImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ProductDaoImpl implements ProductDao {
2525
// @Cacheable(value = "area")
2626
@ReadThroughSingleCache(namespace = "area")
2727
public Product findProduct(@ParameterValueKeyProvider String name) {
28+
// public Product findProduct(String name) {
2829
slowly(); // 고의로 지연시킴
2930
return storage.get(name);
3031
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sample.di.util;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.rubyeye.xmemcached.MemcachedClient;
5+
import net.rubyeye.xmemcached.MemcachedClientBuilder;
6+
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
7+
import net.rubyeye.xmemcached.utils.AddrUtil;
8+
9+
import java.io.IOException;
10+
11+
@Slf4j
12+
public final class MemcachedUtil {
13+
private static MemcachedClientBuilder builder = new XMemcachedClientBuilder(
14+
AddrUtil.getAddresses("localhost:11211"));
15+
16+
private static MemcachedClient client = getMemcachedClient("localhost:11211");
17+
18+
public static MemcachedClient getMemcachedClient(String servers) {
19+
try {
20+
// use text protocol by default
21+
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(servers));
22+
return builder.build();
23+
} catch (IOException e) {
24+
log.error("Create MemcachedClient fail", e);
25+
}
26+
return null;
27+
}
28+
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
3-
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
4-
<Target>System.out</Target>
5-
<encoder>
6-
<pattern>%5p &lt;%d{yyyy-MM-dd HH:mm:ss}&gt;[%C:%L] [%thread] %m%n</pattern>
7-
</encoder>
8-
</appender>
9-
<root level="DEBUG">
10-
<appender-ref ref="stdout"/>
11-
</root>
3+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
4+
<Target>System.out</Target>
5+
<encoder>
6+
<pattern>%5p &lt;%d{yyyy-MM-dd HH:mm:ss}&gt;[%C:%L] [%thread] %m%n</pattern>
7+
</encoder>
8+
</appender>
9+
<logger name="net.rubyeye.xmemcached" level="WARN" additivity="false"/>
10+
<logger name="com.google.code" level="WARN" additivity="false"/>
11+
<root level="DEBUG">
12+
<appender-ref ref="stdout"/>
13+
</root>
1214
</configuration>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sample.di.util;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.rubyeye.xmemcached.MemcachedClient;
5+
import net.rubyeye.xmemcached.exception.MemcachedException;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.concurrent.TimeoutException;
11+
12+
import static org.junit.Assert.*;
13+
14+
@Slf4j
15+
public class MemcachedUtilTest {
16+
17+
MemcachedClient memcachedClient;
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
memcachedClient = MemcachedUtil.getMemcachedClient("localhost:11211");
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
memcachedClient.shutdown();
27+
}
28+
29+
@Test
30+
public void testFlushAll() throws InterruptedException, MemcachedException, TimeoutException {
31+
memcachedClient.set("a", 0, "test:a");
32+
assertEquals("test:a", memcachedClient.get("a"));
33+
34+
memcachedClient.flushAll();
35+
assertNull(memcachedClient.get("a"));
36+
}
37+
}

0 commit comments

Comments
 (0)