Skip to content

Commit c113ab4

Browse files
add module [demo-java-threadSafeCache]
Signed-off-by: ooooo <[email protected]>
1 parent 3d4df33 commit c113ab4

File tree

14 files changed

+222
-3
lines changed

14 files changed

+222
-3
lines changed

spring-boot-annotationProcessor/build.gradle renamed to demo-java-annotationProcessor/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ dependencyManagement {
55
}
66

77
dependencies {
8-
// annotationProcessor project(":spring-boot-annotationProcessor")
9-
// testAnnotationProcessor project(":spring-boot-annotationProcessor")
8+
// annotationProcessor project(":demo-java-annotationProcessor")
9+
// testAnnotationProcessor project(":demo-java-annotationProcessor")
1010
implementation(files("${System.properties['java.home']}/../lib/tools.jar"))
1111

1212
testImplementation('com.google.testing.compile:compile-testing:0.19')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ooooo.cache;
2+
3+
import java.util.function.Supplier;
4+
5+
/**
6+
* @author <a href="https://github.com/ooooo-youwillsee">ooooo</a>
7+
* @since 1.0.0
8+
*/
9+
public interface Cache<T> {
10+
11+
T get(String key);
12+
13+
void put(String key, Supplier<T> supplier);
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.ooooo.cache.impl;
2+
3+
import com.ooooo.cache.Cache;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.function.Supplier;
7+
8+
/**
9+
* @author <a href="https://github.com/ooooo-youwillsee">ooooo</a>
10+
* @since 1.0.0
11+
*/
12+
public class SafeCacheImpl1<T> implements Cache<T> {
13+
14+
private final Map<String, T> map = new HashMap<>();
15+
16+
@Override
17+
public synchronized T get(String key) {
18+
return map.get(key);
19+
}
20+
21+
/**
22+
* 因为使用 synchronized, 导致性能太低了
23+
* 比如 A -> AValue, B -> BValue, 这两个操作应该是互不影响的,
24+
* 但是现在锁是同一个,所以性能不高
25+
*
26+
* @param key
27+
* @param supplier
28+
*/
29+
@Override
30+
public synchronized void put(String key, Supplier<T> supplier) {
31+
if (!map.containsKey(key)) {
32+
map.put(key, supplier.get());
33+
}
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ooooo.cache.impl;
2+
3+
import com.ooooo.cache.Cache;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.function.Supplier;
7+
8+
/**
9+
* @author <a href="https://github.com/ooooo-youwillsee">ooooo</a>
10+
* @since 1.0.0
11+
*/
12+
public class SafeCacheImpl2<T> implements Cache<T> {
13+
14+
private final Map<String, T> map = new HashMap<>();
15+
16+
@Override
17+
public synchronized T get(String key) {
18+
return map.get(key);
19+
}
20+
21+
/**
22+
* 因为使用 synchronized, 导致性能太低了
23+
* 比如 A -> AValue, B -> BValue, 这两个操作应该是互不影响的,
24+
* 但是现在锁是同一个,所以性能不高
25+
*
26+
* @param key
27+
* @param supplier
28+
*/
29+
@Override
30+
public void put(String key, Supplier<T> supplier) {
31+
if (!map.containsKey(key)) {
32+
synchronized (this) {
33+
if (!map.containsKey(key)) {
34+
map.put(key, supplier.get());
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)