Skip to content

Commit fa8fa2c

Browse files
拆分 Cache 和 MapCache ,拆分为使用生成接口和手动存放两种实现
1 parent 1dcfaf9 commit fa8fa2c

9 files changed

Lines changed: 888 additions & 249 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package fybug.nulll.pdcache;
2+
import org.jetbrains.annotations.NotNull;
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.lang.ref.Cleaner;
6+
import java.lang.ref.Reference;
7+
import java.lang.ref.SoftReference;
8+
import java.lang.ref.WeakReference;
9+
10+
import fybug.nulll.pdconcurrent.SyLock;
11+
12+
/**
13+
* <h2>数据缓存通用类.</h2>
14+
* <p>
15+
* 用于缓存单个数据的缓存工具,数据对象可实现 {@link CanClean} 接口返回数据回收时的处理方法<br/>
16+
* 包含缓存的引用,缓存获取方法 {@link #getdata()},缓存回收接口以及并发管理
17+
*
18+
* @author fybug
19+
* @version 0.0.1
20+
* @since PDCache 0.0.1
21+
*/
22+
public abstract
23+
class CacheOb<V> {
24+
/** 缓存引用类型 */
25+
protected final Class<? extends Reference<V>> refClass;
26+
/** 缓存引用 */
27+
protected volatile Reference<V> cache = new WeakReference<>(null);
28+
29+
/** 数据回收接口 */
30+
protected volatile Cleaner.Cleanable cleanable = null;
31+
32+
/** 并发管理 */
33+
protected final SyLock LOCK;
34+
35+
//----------------------------------------------------------------------------------------------
36+
37+
/** 构造缓存,指定缓存方式 */
38+
protected
39+
CacheOb(@NotNull Class<? extends Reference> refc) {this(refc, SyLock.newRWLock()); }
40+
41+
/** 构造缓存,指定缓存方式和并发管理 */
42+
protected
43+
CacheOb(@NotNull Class<? extends Reference> refc, @NotNull SyLock syLock) {
44+
this.refClass = (Class<Reference<V>>) refc;
45+
LOCK = syLock;
46+
}
47+
48+
//----------------------------------------------------------------------------------------------
49+
50+
/**
51+
* 获取缓存
52+
*
53+
* @return 当前缓存的数据
54+
*/
55+
@Nullable
56+
protected
57+
V getdata() throws Exception {
58+
var ref = new Object() {
59+
V item;
60+
};
61+
62+
// 赋予强引用,防止进入回收队列
63+
LOCK.tryread(Exception.class, () -> ref.item = cache.get());
64+
65+
// 校验数据
66+
if (ref.item == null)
67+
// 锁定数据
68+
return LOCK.trywrite(Exception.class, () -> {
69+
/* 梅开二度 */
70+
if ((ref.item = cache.get()) == null) {
71+
// 等待清理
72+
while( cleanable != null )
73+
;
74+
// 无缓存处理
75+
return emptyData();
76+
}
77+
78+
return ref.item;
79+
});
80+
81+
return ref.item;
82+
}
83+
84+
/**
85+
* 无数据时的数据
86+
*
87+
* @return 在没有缓存时返回的数据
88+
*/
89+
@Nullable
90+
protected abstract
91+
V emptyData() throws Exception;
92+
93+
//-----------------------------------
94+
95+
/**
96+
* 放入数据
97+
*
98+
* @param v 数据
99+
*/
100+
protected
101+
void putdata(@Nullable V v) throws Exception {
102+
LOCK.trywrite(Exception.class, () -> {
103+
// 获取对象的回收方法
104+
if (v instanceof CanClean) {
105+
var c = ((CanClean) v).getclean();
106+
// 注册回收方法
107+
cleanable = CacheGcThrea.binClean(v, () -> {
108+
c.run();
109+
cleanable = null;
110+
});
111+
}
112+
113+
// 绑定缓存
114+
cache = refClass.getConstructor(Object.class).newInstance(v);
115+
});
116+
}
117+
118+
/**
119+
* 移除缓存
120+
* <p>
121+
* 强制释放缓存内容<br/>
122+
* 将缓存内容主动加入回收队列
123+
*/
124+
public
125+
void clear() {
126+
LOCK.write(() -> {
127+
// 正在被回收
128+
if (cache.isEnqueued())
129+
return;
130+
/* 释放 */
131+
if (cleanable != null)
132+
cleanable.clean();
133+
// GC mark
134+
cleanable = null;
135+
cache.clear();
136+
});
137+
}
138+
139+
/*--------------------------------------------------------------------------------------------*/
140+
141+
/**
142+
* {@link CacheOb} 子类通用构造工具
143+
* <p>
144+
* 使用 {@link #refernce(Class)} 绑定缓存方式<br/>
145+
* 使用 {@link #lockBy(SyLock)} 绑定并发管理<br/>
146+
* 使用 {@link #build()} 进行构造
147+
*
148+
* @version 0.0.1
149+
* @since CacheOb 0.0.1
150+
*/
151+
@SuppressWarnings( "unchecked" )
152+
protected static abstract
153+
class Build<V, B extends Build<V, B>> {
154+
/** 缓存引用类型 */
155+
protected Class<? extends Reference> refernce = SoftReference.class;
156+
/** 并发管理 */
157+
protected SyLock lockBy = SyLock.newRWLock();
158+
159+
//------------------------------------------------------------------------------------------
160+
161+
/** 设置缓存引用类型 */
162+
@NotNull
163+
public final
164+
B refernce(Class<? extends Reference> refernce) {
165+
this.refernce = refernce;
166+
return (B) this;
167+
}
168+
169+
/** 设置并发管理 */
170+
@NotNull
171+
public final
172+
B lockBy(@NotNull SyLock lockBy) {
173+
this.lockBy = lockBy;
174+
return (B) this;
175+
}
176+
177+
@NotNull
178+
public abstract
179+
CacheOb<V> build();
180+
}
181+
}

src/main/java/fybug/nulll/pdcache/CanClean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author fybug
1010
* @version 0.0.1
1111
* @see Cleanable#register(Object, Runnable)
12-
* @since fun 0.0.1
12+
* @since PDCache 0.0.1
1313
*/
1414
public
1515
interface CanClean {

0 commit comments

Comments
 (0)