Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit 91ad629

Browse files
committed
Renamed function add to set.
Small fixes.
1 parent d21a0d3 commit 91ad629

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

simplecache/src/androidTest/java/com/github/pcpl2/simplecache/SimpleCacheTest.kt

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.github.pcpl2.simplecache
22

33
import android.support.test.InstrumentationRegistry
44
import android.support.test.runner.AndroidJUnit4
5+
import android.util.Log
56
import com.github.pcpl2.simplecache.models.TestObject
67

78
import org.junit.Test
@@ -26,71 +27,71 @@ class SimpleCacheTest {
2627

2728
val cacheManager = CacheManager.createInstance(appContext, null)
2829

29-
cacheManager.add("String", "Hello World")
30-
cacheManager.add("Int", 255)
31-
cacheManager.add("Bool", false)
32-
cacheManager.add("float", 5.55)
30+
cacheManager.set("String", "Hello World")
31+
cacheManager.set("Int", 255)
32+
cacheManager.set("Bool", false)
33+
cacheManager.set("float", 5.55)
3334
val testMap = HashMap<String, String>()
3435
testMap["testKey"] = "TestValue"
35-
cacheManager.add("map", testMap)
36+
cacheManager.set("map", testMap)
3637
val testObject = TestObject(69, "abababa", false, 6.66f, testMap)
37-
cacheManager.add("obj", testObject)
38+
cacheManager.set("obj", testObject)
3839
val testObject2 = TestObject(885, "Testing two!", true, 3.14f, testMap)
3940

40-
cacheManager.add("obj2", testObject2)
41+
cacheManager.set("obj2", testObject2)
4142

4243
cacheManager.get(key = "String", success = { value, type ->
4344
assertEquals("Hello World", value)
44-
assert(type!!.isInstance(String::class))
45-
System.out.println(value.toString())
45+
assert(type.isInstance(String::class))
46+
Log.d("simpleCacheTest", value.toString())
4647
})
4748

48-
cacheManager.get(key = "Int", success = { value, type ->
49+
cacheManager.get(key = "Int", checkExpired = true, success = { value, type ->
4950
assertEquals(255, value)
50-
assert(type!!.isInstance(Int::class))
51-
System.out.println(value.toString())
51+
assert(type.isInstance(Int::class))
52+
Log.d("simpleCacheTest", value.toString())
5253
})
5354

5455
cacheManager.get(key = "Bool", success = { value, type ->
5556
assertEquals(false, value)
56-
assert(type!!.isInstance(Boolean::class))
57-
System.out.println(value.toString())
57+
assert(type.isInstance(Boolean::class))
58+
Log.d("simpleCacheTest", value.toString())
5859
})
5960

6061
cacheManager.get(key = "float", success = { value, type ->
6162
assertEquals(5.55, value)
62-
assert(type!!.isInstance(Float::class))
63-
System.out.println(value.toString())
63+
assert(type.isInstance(Float::class))
64+
Log.d("simpleCacheTest", value.toString())
6465
})
6566

6667
cacheManager.get(key = "map", success = { value, type ->
6768
assertEquals(testMap, value)
68-
assert(type!!.isInstance(HashMap::class))
69-
System.out.println(value.toString())
69+
assert(type.isInstance(HashMap::class))
70+
Log.d("simpleCacheTest", value.toString())
7071
})
7172

7273
cacheManager.get(key = "obj", success = { value, type ->
7374
assertEquals(testObject, value)
74-
assert(type!!.isInstance(TestObject::class))
75-
System.out.println(value.toString())
75+
assert(type.isInstance(TestObject::class))
76+
Log.d("simpleCacheTest", value.toString())
7677
})
7778

7879
cacheManager.get(key = "obj2", success = { value, type ->
7980
assertEquals(testObject2, value)
80-
assert(type!!.isInstance(TestObject::class))
81-
System.out.println(value.toString())
81+
assert(type.isInstance(TestObject::class))
82+
Log.d("simpleCacheTest", value.toString())
8283
})
8384

8485
cacheManager.remove("obj2")
8586

8687
cacheManager.get(key = "obj2", success = { _, _ ->
8788

8889
}, error = {
89-
System.out.println("obj2 is not exist.")
90+
Log.d("simpleCacheTest", "obj2 is not exist.")
9091
assert(true)
9192
})
9293

93-
System.out.println(CacheManager.getListOfCacheFiles(appContext).toString())
94+
Log.d("simpleCacheTest", CacheManager.getListOfCacheFiles(appContext).toString())
9495

9596
assertEquals("com.github.pcpl2.simplecache.test", appContext.packageName)
9697
}

simplecache/src/main/java/com/github/pcpl2/simplecache/CacheManagerImpl.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class CacheManagerImpl(private val context: Context, fileName: String?) {
4747
}
4848

4949
/**
50-
* Add element to cache.
50+
* Add or update element in cache.
5151
*
5252
* @param key The key under which the added element will be available.
5353
* @param value Element that is added. can be of any type.
5454
* @param lifeTime Element lifetime in cache (given in seconds). If it is zero then there is no life time.
5555
*/
56-
fun add(key: String, value: Any, lifeTime: Long = 0) {
56+
fun set(key: String, value: Any, lifeTime: Long = 0) {
5757
backgroundSaveFileThread?.join()
5858
val cacheEntry = CacheEntry(ts = DateTime.now(), lifeTime = lifeTime, value = value, type = value.javaClass.name)
5959
cahceMap[key] = cacheEntry
@@ -68,11 +68,10 @@ class CacheManagerImpl(private val context: Context, fileName: String?) {
6868
* @param success Callback returning element and element type from cache. If it does not exist, the element and type returned are null.
6969
* @param error Callback running if element with key not exist in map or lifetime of element is end.
7070
*/
71-
fun get(key: String, checkExpired: Boolean = true, success: (value: Any?, type: Class<*>?) -> Unit, error: (() -> Unit)? = null) {
71+
fun get(key: String, checkExpired: Boolean = true, success: (value: Any, type: Class<*>) -> Unit, error: (() -> Unit)? = null) {
7272
backgroundReadFileThread?.join()
7373

7474
val entry = cahceMap[key]
75-
7675
if (entry != null) {
7776
val classType = Class.forName(entry.type)
7877
val valueTyped = classType.cast(entry.value)
@@ -121,8 +120,8 @@ class CacheManagerImpl(private val context: Context, fileName: String?) {
121120
backgroundSaveFileThread?.join()
122121
backgroundSaveFileThread = Thread(Runnable {
123122
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
124-
val json = gson.toJson(cahceMap)
125-
Log.d("SaveJson", json)
123+
val json = gson.toJson(cahceMap.toMap())
124+
//Log.d("SaveJson", json)
126125
val file = File(context.cacheDir, "${CacheManager.directoryName}${File.separator}$filename")
127126
val fw = FileWriter(file.absoluteFile)
128127
val bw = BufferedWriter(fw)

0 commit comments

Comments
 (0)