Skip to content

Commit 7c81f2f

Browse files
committed
disklrucache added to storage
1 parent 175c56a commit 7c81f2f

File tree

1 file changed

+61
-9
lines changed

1 file changed

+61
-9
lines changed

_drafts/2019-08-12-storage.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,84 @@ Alternatywnym podejściem do implementacji pamięci podręcznej w stosunku do st
154154
{% highlight kotlin %}
155155
class LruCacheActivity : AppCompatActivity() {
156156

157-
//set key and value type
157+
//set key and value type, it could be something heavy like Bitmap
158158
private lateinit var memoryCache : LruCache<String, String>
159+
private lateinit var diskCache : DiskLruCache //external dependency
159160

160161
override fun onCreate(savedInstanceState: Bundle?) {
161162
super.onCreate(savedInstanceState)
162-
163+
164+
//init cache before
163165
initMemoryCache()
164-
useMemoryCache() //use some put, get, remove, resize method in memory
166+
initDiskCache()
167+
168+
//combine memory and disk cache
169+
writeToCache("key", "value")
170+
readFromCache("key")
165171
}
166172

167173
fun initMemoryCache() {
168174
//calculate max memory to 10% of available memory
169175
val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()
170-
176+
171177
memoryCache = object: LruCache<String, String>(maxMemory / 10) {
172178
override fun sizeOf(key: String, value: String): Int {
173179
return value.toByteArray().size
174180
}
175181
}
176182
}
183+
184+
fun initDiskCache() {
185+
val directory = File(cacheDir.path + File.separator + "lru") //use internal or external
186+
val appVersion = 1 //
187+
val valueCount = 1 //counter entries for every key
188+
val maxSize = 1024L * 1024L * 10L //10MB of the cache
189+
diskCache = DiskLruCache.open(directory, appVersion, valueCount, maxSize)
190+
}
177191

178-
fun useMemoryCache() {
179-
memoryCache.put("key", "some content like json")
180-
val value = memoryCache.get("key")
181-
memoryCache.remove("key")
182-
memoryCache.resize(memoryCache.size() * 2)
192+
fun writeToCache(key : String, value : String) {
193+
if(readFromMemoryCache(key) == null) {
194+
writeToMemoryCache(key, value)
195+
}
196+
writeToDiskCache(key, value)
197+
}
198+
199+
fun readFromCache(key : String) : String? {
200+
val result = readFromMemoryCache(key)
201+
return if(result == null) {
202+
readFromDiskCache(key)
203+
} else result
204+
}
205+
206+
fun writeToMemoryCache(key : String, value : String) {
207+
memoryCache.put(key, value)
208+
}
209+
210+
fun readFromMemoryCache(key : String) : String? {
211+
return memoryCache.get(key)
212+
}
213+
214+
fun writeToDiskCache(key : String, value : String) {
215+
val editor : DiskLruCache.Editor? = diskCache.edit(key) //returns null if there is active editing
216+
//begin the transaction for this key
217+
editor?.let {
218+
//the entry is mark as being edited
219+
val output = it.newOutputStream(0)
220+
output.write(value.toByteArray())
221+
it.commit() //transaction has been finished
222+
}
223+
}
224+
225+
fun readFromDiskCache(key : String) : String? {
226+
var value : String? = null
227+
val snapshot : DiskLruCache.Snapshot? = diskCache.get(key) //returns null if entries doesn't exists
228+
snapshot?.let {
229+
val input = it.getInputStream(0)
230+
val bytes = input.readBytes()
231+
value = String(bytes) //decode bytes
232+
it.close()
233+
}
234+
return value
183235
}
184236
}
185237
{% endhighlight %}

0 commit comments

Comments
 (0)