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

Commit 9eb8ccd

Browse files
authored
Merge pull request #1 from pcpl2/feature/1.2.0
Feature/1.2.0
2 parents 859f4bf + 59f4a96 commit 9eb8ccd

6 files changed

Lines changed: 362 additions & 49 deletions

File tree

README.md

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# SimpleCache
22
[![Build Status](https://travis-ci.org/pcpl2/CacheLib.svg?branch=master)](https://travis-ci.org/pcpl2/CacheLib)
33
[![SimpleCache](https://api.bintray.com/packages/pcpl2/maven/simplecache/images/download.svg) ](https://bintray.com/pcpl2/maven/simplecache/_latestVersion)
4-
[![API](https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=plastic)](https://android-arsenal.com/api?level=21)
54
[![License](https://img.shields.io/:license-apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
5+
[![API](https://img.shields.io/badge/API-14%2B-green.svg?style=flat)](https://android-arsenal.com/api?level=21)
66
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-simplecache-green.svg?style=flat )]( https://android-arsenal.com/details/1/6965 )
77

88
A simple library for saving data in the cache and reading them.
@@ -12,7 +12,7 @@ The library is hosted on jcenter. To use it, add the following to your module le
1212

1313
```gradle
1414
dependencies {
15-
implementation 'com.github.pcpl2:simplecache:1.1.0'
15+
implementation 'com.github.pcpl2:simplecache:1.2.0'
1616
}
1717
```
1818

@@ -21,17 +21,22 @@ dependencies {
2121
**To init instance of cache manager without filename:**
2222

2323
```kotlin
24-
val cacheManager = CacheManager.createInstance(appContext, null)
24+
val cacheManager = CacheManager.createInstance(context = appContext)
2525
```
2626
The `cacheManager` instance usage default cache file.
2727

2828
**To init instance of cache manager with filename:**
2929

3030
```kotlin
31-
val cacheManager = CacheManager.createInstance(appContext, "filesCache")
31+
val cacheManager = CacheManager.createInstance(context = appContext, fileName = "filesCache")
3232
```
3333
The `cacheManager` instance usage cache with file name `filesCache`.
3434

35+
**To init instance of cache manager without autosave:**
36+
```kotlin
37+
val cacheManager = CacheManager.createInstance(context = appContext, autoSave = false)
38+
```
39+
If you have deactivated the automatic saving, you must remember to save the data using the `save` method.
3540

3641
## Add or update elements to cache:
3742
The `set` function accepts 3 parameters: `key: Stirng, value: Any, lifetime: Long`.
@@ -55,6 +60,12 @@ cacheManager.set("IntValueKey", 255)
5560
cacheManager.set("BooleanKey", false)
5661
cacheManager.set("FloatKey", 5.55)
5762
```
63+
## Save cache data
64+
**To save cache on disk (if autosave disabled):**
65+
66+
```kotlin
67+
cacheManager.save()
68+
```
5869

5970
## Getting element from cache:
6071
The `get` function accepts 4 parameters: `key: Stirng, checkExpired: Boolean = true, success: (value: Any, type: Class<*>) -> Unit, error: (() -> Unit)? = null`.
@@ -116,13 +127,13 @@ cacheManager.get(key = "FloatKey", checkExpired = false, success = { value, type
116127
## Remove element from cache
117128
The `remove` function accept 1 parameter: `key: Stirng`.
118129

119-
120130
**To remove element from cache instance:**
121131
```kotlin
122132
cacheManager.remove("FloatKey")
123133
```
124134

125135
## Clear cache instance
136+
126137
The `removeAllElements` function cleans the entire cache.
127138

128139
**To clear cache istance:**
@@ -131,6 +142,16 @@ The `removeAllElements` function cleans the entire cache.
131142
cacheManager.removeAllElements()
132143
```
133144

145+
**To remove all elements in cache istance:**
146+
147+
```kotlin
148+
cacheManager.removeAllElements()
149+
```
150+
151+
The `dispose` function accept 1 parameter: `save: Boolean = true`.
152+
153+
The save parameter is optional, default set as true.
154+
134155
## List of the cache files.
135156
The `getListOfCacheFiles` function accept 1 parameter: `ctx: Context` and return list of exist cache files.
136157

@@ -140,6 +161,49 @@ The `getListOfCacheFiles` function accept 1 parameter: `ctx: Context` and return
140161
CacheManager.getListOfCacheFiles(appContext)
141162
```
142163

164+
# Global instances
165+
166+
## Create global instacne
167+
The `createGlobalInstance` function accept 4 parameters: `context: Context, instanceName: String, fileName: String = "CacheBase", autoSave: Boolean = true` and return cache instance.
168+
169+
**To create global instance:**
170+
171+
```kotlin
172+
CacheManager.createGlobalInstance(context = appContext, instanceName = "Instance1", fileName = "instanceFile1")
173+
```
174+
175+
## Get global instacne
176+
The `getGlobalInstance` function accept 1 parameter: `instanceName: String` and return cache instance.
177+
178+
**To get global instance by name:**
179+
180+
```kotlin
181+
val globalInstance2 = CacheManager.getGlobalInstance(instanceName = "Instance1")
182+
```
183+
184+
**To get list of global instance names:**
185+
186+
```kotlin
187+
var listOfIInstancesNames = CacheManager.getListOfGlobalInstanceNames()
188+
```
189+
190+
## Remove global instance
191+
The `removeGlobalInstance` function accept 2 parameters: `instanceName: String, save: Boolean = true`.
192+
193+
The save parameter is optional, default set as true.
194+
195+
**To remove global instance by name:**
196+
197+
```kotlin
198+
val globalInstance2 = CacheManager.removeGlobalInstance(instanceName = "Instance1")
199+
```
200+
201+
**To remove all global instances:**
202+
203+
```kotlin
204+
val globalInstance2 = CacheManager.removeAllGlobalInstances()
205+
```
206+
143207
# Changelog
144208
Please see the [Changelog](https://github.com/pcpl2/CacheLib/wiki/Changelog) page to see what's recently changed.
145209

@@ -159,4 +223,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
159223
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
160224
See the License for the specific language governing permissions and
161225
limitations under the License.
162-
```
226+
```

simplecache/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ android {
55
compileSdkVersion 27
66

77
defaultConfig {
8-
minSdkVersion 21
8+
minSdkVersion 14
99
targetSdkVersion 27
1010
versionCode 1
11-
versionName "1.1.0"
11+
versionName "1.2.0"
1212

1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1414

@@ -40,7 +40,7 @@ ext {
4040
siteUrl = 'https://github.com/pcpl2/CacheLib'
4141
gitUrl = 'https://github.com/pcpl2/CacheLib.git'
4242

43-
libraryVersion = '1.1.0'
43+
libraryVersion = '1.2.0'
4444

4545
developerId = 'pcpl2'
4646
developerName = 'Patryk Ławicki'
@@ -58,8 +58,8 @@ dependencies {
5858
implementation 'com.google.code.gson:gson:2.8.2'
5959

6060
testImplementation 'junit:junit:4.12'
61-
androidTestImplementation 'com.android.support.test:runner:1.0.1'
62-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
61+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
62+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
6363
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
6464
}
6565

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

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,85 @@ import org.junit.Assert.*
1919
*/
2020
@RunWith(AndroidJUnit4::class)
2121
class SimpleCacheTest {
22+
23+
private val appContext = InstrumentationRegistry.getTargetContext()!!
24+
2225
@Test
2326
@Throws(Exception::class)
24-
fun simpleCacheTest() {
27+
fun cacheWithAutoSaveTest() {
28+
val cacheManager = CacheManager.createInstance(context = appContext)
29+
30+
cacheManager.set("String", "Hello World")
31+
cacheManager.set("Int", 255)
32+
cacheManager.set("Bool", false)
33+
cacheManager.set("float", 5.55)
34+
val testMap = HashMap<String, String>()
35+
testMap["testKey"] = "TestValue"
36+
cacheManager.set("map", testMap)
37+
val testObject = TestObject(69, "Test String", false, 6.66f, testMap)
38+
cacheManager.set("obj", testObject)
39+
val testObject2 = TestObject(885, "Testing two!", true, 3.14f, testMap)
40+
41+
cacheManager.set("obj2", testObject2)
42+
43+
cacheManager.get(key = "String", success = { value, type ->
44+
assertEquals("Hello World", value)
45+
assert(type.isInstance(String::class))
46+
Log.d("simpleCacheTest", value.toString())
47+
})
48+
49+
cacheManager.get(key = "Int", checkExpired = true, success = { value, type ->
50+
assertEquals(255, value)
51+
assert(type.isInstance(Int::class))
52+
Log.d("simpleCacheTest", value.toString())
53+
})
54+
55+
cacheManager.get(key = "Bool", success = { value, type ->
56+
assertEquals(false, value)
57+
assert(type.isInstance(Boolean::class))
58+
Log.d("simpleCacheTest", value.toString())
59+
})
60+
61+
cacheManager.get(key = "float", success = { value, type ->
62+
assertEquals(5.55, value)
63+
assert(type.isInstance(Float::class))
64+
Log.d("simpleCacheTest", value.toString())
65+
})
66+
67+
cacheManager.get(key = "map", success = { value, type ->
68+
assertEquals(testMap, value)
69+
assert(type.isInstance(HashMap::class))
70+
Log.d("simpleCacheTest", value.toString())
71+
})
72+
73+
cacheManager.get(key = "obj", success = { value, type ->
74+
assertEquals(testObject, value)
75+
assert(type.isInstance(TestObject::class))
76+
Log.d("simpleCacheTest", value.toString())
77+
})
78+
79+
cacheManager.get(key = "obj2", success = { value, type ->
80+
assertEquals(testObject2, value)
81+
assert(type.isInstance(TestObject::class))
82+
Log.d("simpleCacheTest", value.toString())
83+
})
84+
85+
cacheManager.remove("obj2")
2586

26-
val appContext = InstrumentationRegistry.getTargetContext()
87+
cacheManager.get(key = "obj2", success = { _, _ ->
2788

28-
val cacheManager = CacheManager.createInstance(appContext, null)
89+
}, error = {
90+
Log.d("simpleCacheTest", "obj2 is not exist.")
91+
assert(true)
92+
})
93+
94+
cacheManager.removeAllElements()
95+
}
96+
97+
@Test
98+
@Throws(Exception::class)
99+
fun cacheWithoutAutoSaveTest() {
100+
val cacheManager = CacheManager.createInstance(context = appContext, fileName = "NoAutoSave", autoSave = false)
29101

30102
cacheManager.set("String", "Hello World")
31103
cacheManager.set("Int", 255)
@@ -34,12 +106,14 @@ class SimpleCacheTest {
34106
val testMap = HashMap<String, String>()
35107
testMap["testKey"] = "TestValue"
36108
cacheManager.set("map", testMap)
37-
val testObject = TestObject(69, "abababa", false, 6.66f, testMap)
109+
val testObject = TestObject(69, "Test String", false, 6.66f, testMap)
38110
cacheManager.set("obj", testObject)
39111
val testObject2 = TestObject(885, "Testing two!", true, 3.14f, testMap)
40112

41113
cacheManager.set("obj2", testObject2)
42114

115+
cacheManager.save()
116+
43117
cacheManager.get(key = "String", success = { value, type ->
44118
assertEquals("Hello World", value)
45119
assert(type.isInstance(String::class))
@@ -91,8 +165,46 @@ class SimpleCacheTest {
91165
assert(true)
92166
})
93167

94-
Log.d("simpleCacheTest", CacheManager.getListOfCacheFiles(appContext).toString())
168+
cacheManager.removeAllElements()
169+
}
170+
171+
@Test
172+
fun cacheListFilesTest() {
173+
val files = CacheManager.getListOfCacheFiles(appContext)
174+
Log.d("simpleCacheTest",files.toString())
175+
176+
assert(files.size == 2)
177+
}
178+
179+
@Test
180+
fun globalInstancesTest() {
181+
val instanceName1 = "Instance1"
182+
val instanceName2 = "Instance2"
183+
184+
val instance1 = CacheManager.createGlobalInstance(context = appContext, instanceName = instanceName1, fileName = "instanceFile1")
185+
val instance2 = CacheManager.createGlobalInstance(context = appContext, instanceName = instanceName2, fileName = "InstanceFile2")
186+
187+
val globalInstance1 = CacheManager.getGlobalInstance(instanceName1)
188+
val globalInstance2 = CacheManager.getGlobalInstance(instanceName2)
189+
190+
assert(instance1 == globalInstance1)
191+
assert(instance1 != globalInstance2)
192+
assert(instance2 == globalInstance2)
193+
194+
var listOfIInstancesNames = CacheManager.getListOfGlobalInstanceNames()
195+
196+
assert(listOfIInstancesNames.contains(instanceName2))
197+
198+
CacheManager.removeGlobalInstance(instanceName1)
199+
200+
listOfIInstancesNames = CacheManager.getListOfGlobalInstanceNames()
201+
202+
assert(!listOfIInstancesNames.contains(instanceName1))
203+
204+
CacheManager.removeAllGlobalInstances()
205+
206+
listOfIInstancesNames = CacheManager.getListOfGlobalInstanceNames()
95207

96-
assertEquals("com.github.pcpl2.simplecache.test", appContext.packageName)
208+
assert(listOfIInstancesNames.isEmpty())
97209
}
98210
}

0 commit comments

Comments
 (0)