12
12
import java .util .Properties ;
13
13
import java .util .concurrent .Executors ;
14
14
import java .util .concurrent .ScheduledExecutorService ;
15
+ import java .util .concurrent .atomic .AtomicLong ;
15
16
import java .util .logging .Logger ;
16
17
17
18
/**
@@ -46,8 +47,8 @@ public class CachedClient extends BasicKVDatabaseClient {
46
47
private int expireNumThread ;
47
48
private int hmapConcurrency ;
48
49
private String dbClassName ;
49
- private long queryCount = 0L ;
50
- private long hitCount = 0L ;
50
+ private AtomicLong queryCount = new AtomicLong ( 0L ) ;
51
+ private AtomicLong hitCount = new AtomicLong ( 0L ) ;
51
52
private Logger logger = Logger .getLogger (this .getClass ().getName ());
52
53
53
54
@@ -59,14 +60,14 @@ public class CachedClient extends BasicKVDatabaseClient {
59
60
@ Override
60
61
public byte [] get (byte [] key ) throws Exception {
61
62
assert cache != null ;
62
- queryCount ++ ;
63
+ this . queryCount . getAndAdd ( 1L ) ;
63
64
byte [] result = cache .get (key );
64
65
if (result == null ) {
65
66
result = db .get (key );
66
67
if (result != null )
67
68
cache .put (key , result );
68
69
} else {
69
- hitCount ++ ;
70
+ this . hitCount . getAndAdd ( 1L ) ;
70
71
}
71
72
return result ;
72
73
}
@@ -75,17 +76,21 @@ public byte[] get(byte[] key) throws Exception {
75
76
public byte [][] getAll (byte keys [][]) throws Exception {
76
77
byte [][] results = new byte [keys .length ][];
77
78
IntArrayList queryKeysIDs = new IntArrayList ();
79
+ long localQueryCount = 0L ;
80
+ long localHitCount = 0L ;
78
81
79
82
for (int i = 0 ; i < keys .length ; i ++) {
80
- queryCount ++;
83
+ localQueryCount ++;
81
84
byte [] result = cache .get (keys [i ]);
82
85
if (result == null ) {
83
86
queryKeysIDs .add (i );
84
87
} else {
85
88
results [i ] = result ;
86
- hitCount ++;
89
+ localHitCount ++;
87
90
}
88
91
}
92
+ this .queryCount .addAndGet (localQueryCount );
93
+ this .hitCount .addAndGet (localHitCount );
89
94
byte [][] queryKeys = new byte [queryKeysIDs .size ()][];
90
95
for (int i = 0 ; i < queryKeysIDs .size (); i ++) {
91
96
queryKeys [i ] = keys [queryKeysIDs .getInt (i )];
@@ -140,6 +145,7 @@ private void loadConfigurations(Properties conf) {
140
145
this .hmapConcurrency = Integer .valueOf (conf .getProperty (CONF_HMAP_CONCURRENCY , DEFAULT_HMAP_CONCURRENCY ));
141
146
this .dbClassName = conf .getProperty (CONF_DB_BACKEND_CLASS_NAME );
142
147
logger .info ("Get configurations:" + conf );
148
+ logger .info ("Set cache capacity: " + this .cacheCapacityInBytes + " bytes" );
143
149
}
144
150
145
151
/**
@@ -192,8 +198,8 @@ public CacheStatsReportRunnable(long sleepTimeInSecond) {
192
198
private void writeStatsToFile () {
193
199
try {
194
200
PrintWriter writer = new PrintWriter (new File (statsFilePath ));
195
- long missCount = queryCount - hitCount ;
196
- writer .println (String .format ("CacheStats{hitCount=%d, missCount=%d, }" , hitCount , missCount ));
201
+ long missCount = queryCount . get () - hitCount . get () ;
202
+ writer .println (String .format ("CacheStats{hitCount=%d, missCount=%d, }" , hitCount . get () , missCount ));
197
203
writer .close ();
198
204
} catch (FileNotFoundException e ) {
199
205
e .printStackTrace ();
0 commit comments