GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactoring: make DefaultSessionAwareExpirableDataManager more eazy t… · astamuse/asta4d@7c86274

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • asta4d-web/src/main/java/com/astamuse/asta4d/web/util/timeout
    • DefaultSessionAwareExpirableDataManager.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎asta4d-web/src/main/java/com/astamuse/asta4d/web/util/timeout/DefaultSessionAwareExpirableDataManager.java‎

Copy file name to clipboardExpand all lines: asta4d-web/src/main/java/com/astamuse/asta4d/web/util/timeout/DefaultSessionAwareExpirableDataManager.java
+44-17Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package com.astamuse.asta4d.web.util.timeout;
1919

20+
import java.io.Serializable;
2021
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.Map.Entry;
2325
import java.util.concurrent.ConcurrentHashMap;
2426
import java.util.concurrent.Executors;
@@ -36,7 +38,7 @@ public class DefaultSessionAwareExpirableDataManager implements ExpirableDataMan
3638

3739
private static final String CheckSessionIdKey = DefaultSessionAwareExpirableDataManager.class + "#CheckSessionIdKey";
3840

39-
private ConcurrentHashMap<String, DataHolder> dataMap = null;
41+
private Map<String, DataHolder> dataMap = null;
4042

4143
private AtomicInteger dataCounter = null;
4244

@@ -57,7 +59,8 @@ public class DefaultSessionAwareExpirableDataManager implements ExpirableDataMan
5759
private String checkThreadName = this.getClass().getSimpleName() + "-check-thread";
5860

5961
public DefaultSessionAwareExpirableDataManager() {
60-
62+
dataMap = createThreadSafeDataMap();
63+
dataCounter = new AtomicInteger();
6164
}
6265

6366
public void setExpirationCheckPeriodInMilliseconds(long expirationCheckPeriodInMilliseconds) {
@@ -80,11 +83,28 @@ public void setMaxSpinTimeInMilliseconds(long maxSpinTimeInMilliseconds) {
8083
this.maxSpinTimeInMilliseconds = maxSpinTimeInMilliseconds;
8184
}
8285

86+
protected Map<String, DataHolder> createThreadSafeDataMap() {
87+
return new ConcurrentHashMap<>();
88+
}
89+
90+
protected void decreaseCount() {
91+
dataCounter.decrementAndGet();
92+
}
93+
94+
protected void addCount(int delta) {
95+
dataCounter.addAndGet(delta);
96+
}
97+
98+
protected void increaseCount() {
99+
dataCounter.incrementAndGet();
100+
}
101+
102+
protected int getCurrentCount() {
103+
return dataCounter.get();
104+
}
105+
83106
@Override
84107
public void start() {
85-
// init fields
86-
dataMap = new ConcurrentHashMap<>();
87-
dataCounter = new AtomicInteger();
88108
service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
89109
@Override
90110
public Thread newThread(Runnable r) {
@@ -109,7 +129,7 @@ public void run() {
109129
}
110130
}
111131
if (removedCounter > 0) {
112-
dataCounter.addAndGet(-removedCounter);
132+
addCount(-removedCounter);
113133
}
114134
}
115135
}, expirationCheckPeriodInMilliseconds, expirationCheckPeriodInMilliseconds, TimeUnit.MILLISECONDS);
@@ -129,7 +149,7 @@ public <T> T get(String dataId, boolean remove) {
129149
if (remove) {
130150
holder = dataMap.remove(dataId);
131151
if (holder != null) {
132-
dataCounter.decrementAndGet();
152+
decreaseCount();
133153
if (holder.isExpired(System.currentTimeMillis())) {
134154
holder = null;
135155
}
@@ -140,7 +160,7 @@ public <T> T get(String dataId, boolean remove) {
140160
if (holder.isExpired(System.currentTimeMillis())) {
141161
holder = dataMap.remove(dataId);
142162
if (holder != null) {
143-
dataCounter.decrementAndGet();
163+
decreaseCount();
144164
holder = null;
145165
}
146166
}
@@ -158,13 +178,20 @@ public <T> T get(String dataId, boolean remove) {
158178
}
159179

160180
public void put(String dataId, Object data, long expireMilliSeconds) {
161-
if (dataCounter.get() >= maxDataSize) {
181+
Object existing = dataMap.put(dataId, new DataHolder(data, expireMilliSeconds, retrieveSessionId(true)));
182+
if (existing == null) {
183+
increaseCount();
184+
}
185+
}
186+
187+
protected void checkSize() {
188+
if (getCurrentCount() >= maxDataSize) {
162189
try {
163190
long spinTimeTotal = 0;
164-
while (dataCounter.get() >= maxDataSize) {
191+
while (getCurrentCount() >= maxDataSize) {
165192
if (spinTimeTotal >= maxSpinTimeInMilliseconds) {
166-
String msg = "There are too many data in %s and we could not get empty space after waiting for %d milliseconds."
167-
+ " The configured max size is %d and perhaps you should increase the value.";
193+
String msg = "There are too many data in %s and we could not get empty space after waiting for %d milliseconds." +
194+
" The configured max size is %d and perhaps you should increase the value.";
168195
msg = String.format(msg, this.getClass().getName(), spinTimeTotal, maxDataSize);
169196
throw new TooManyDataException(msg);
170197
}
@@ -175,10 +202,6 @@ public void put(String dataId, Object data, long expireMilliSeconds) {
175202
throw new RuntimeException(e);
176203
}
177204
}
178-
Object existing = dataMap.put(dataId, new DataHolder(data, expireMilliSeconds, retrieveSessionId(true)));
179-
if (existing == null) {
180-
dataCounter.incrementAndGet();
181-
}
182205
}
183206

184207
protected String retrieveSessionId(boolean create) {
@@ -200,7 +223,11 @@ protected void finalize() throws Throwable {
200223
super.finalize();
201224
}
202225

203-
private static class DataHolder {
226+
protected static class DataHolder implements Serializable {
227+
/**
228+
*
229+
*/
230+
private static final long serialVersionUID = 1L;
204231
private final Object data;
205232
private final long creationTime;
206233
private final long expireMilliSeconds;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.