-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcacher.java
218 lines (192 loc) · 4.96 KB
/
memcacher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.askquickly;
// stored in memory, very fast, available only while app is running
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Events;
@Author("memCache")
@Version(0.01f)
@ShortName("memcacher")
@Events(values={"expired (response As String)"})
public class memcacher implements StorageInterface {
private static String eventName;
private static BA ba;
@Hide
volatile protected Map<String, Object> map;
@Hide
volatile protected long lastModified;
public void Initialize(final BA ba, String evname) {
this.ba = ba;
this.eventName = evname.toLowerCase(BA.cul);
}
public memcacher()
{
map = getMap();
lastModified = 0;
}
@Hide
protected Map<String, Object> getMap()
{
return new HashMap<String, Object>();
}
synchronized public boolean containsKey(String key)
{
return (map.containsKey(key));
}
synchronized public void clearAll()
{
map = null;
map = getMap();
updateLastModified();
}
synchronized public void clear(String key)
{
map.remove(key);
}
synchronized public Object get(String key)
{
if (map.containsKey(key)) {
return map.get(key);
} else {
return null;
}
}
synchronized public boolean getBoolean(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Boolean)value;
} catch (Exception e) {}
}
return false;
}
synchronized public byte getByte(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Byte)value;
} catch (Exception e) {}
}
return 0;
}
synchronized public double getDouble(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Double)value;
} catch (Exception e) {}
}
return 0;
}
synchronized public float getFloat(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Float)value;
} catch (Exception e) {}
}
return 0;
}
synchronized public int getInt(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Integer)value;
} catch (Exception e) {}
}
return 0;
}
synchronized public long getLong(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (Long)value;
} catch (Exception e) {}
}
return 0;
}
synchronized public String getString(String key)
{
if (map.containsKey(key)) {
Object value = map.get(key);
try {
return (String)value;
} catch (Exception e) {}
}
return null;
}
synchronized public void putByte(String key, byte value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putInt(String key, int value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putLong(String key, long value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putBoolean(String key, boolean value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putFloat(String key, float value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putDouble(String key, double value)
{
map.put(key, value);
updateLastModified();
}
synchronized public void putstrings(String key, String value)
{
map.put(key, value);
updateLastModified();
}
protected void updateLastModified()
{
lastModified = Calendar.getInstance().getTimeInMillis();
}
synchronized public long getLastModified()
{
return lastModified;
}
synchronized public void cacheExpire(long time)
{
Timer timerObj = new Timer();
TimerTask timerTaskObj = new TimerTask() {
public void run() {
if(ba.subExists(memcacher.eventName + "_expired"))
{
//map.remove(Keyy); // do a test here
ba.raiseEvent(this,memcacher.eventName + "_expired", "Memory wipe");
}
else
{
BA.LogError("event sub does not exist: " + memcacher.eventName);
}
}
};
timerObj.schedule(timerTaskObj, 0, time);
}
}