|
1 | | -# CacheLib [](https://travis-ci.org/pcpl2/CacheLib) |
| 1 | +# SimpleCache |
| 2 | +[](https://travis-ci.org/pcpl2/CacheLib) |
| 3 | +[ ](https://bintray.com/pcpl2/maven/simplecache/_latestVersion) |
| 4 | +[](https://android-arsenal.com/api?level=21) |
| 5 | + |
| 6 | +A simple library for saving data in the cache and reading them. |
| 7 | + |
| 8 | +# Setup |
| 9 | +The library is hosted on jcenter. To use it, add the following to your module level `build.gradle` file's dependencies: |
| 10 | + |
| 11 | +```gradle |
| 12 | +dependencies { |
| 13 | + implementation 'com.github.pcpl2:simplecache:1.0.0' |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +# Basic usage |
| 18 | + |
| 19 | +**To init instance of cache manager without filename:** |
| 20 | + |
| 21 | +```kotlin |
| 22 | +val cacheManager = CacheManager.createInstance(appContext, null) |
| 23 | +``` |
| 24 | +The `cacheManager` instance usage default cache file. |
| 25 | + |
| 26 | +**To init instance of cache manager with filename:** |
| 27 | + |
| 28 | +```kotlin |
| 29 | +val cacheManager = CacheManager.createInstance(appContext, "filesCache") |
| 30 | +``` |
| 31 | +The `cacheManager` instance usage cache with file name `filesCache`. |
| 32 | + |
| 33 | + |
| 34 | +## Add elements to cache: |
| 35 | +The `add` function accepts 3 parameters: `key: Stirng, value: Any, lifetime: Long`. |
| 36 | + |
| 37 | +The lifetime parameter is the lifetime in seconds and is optional, default setted to 0 (no lifetime). |
| 38 | + |
| 39 | +**To add element to cache instance with set lifetime:** |
| 40 | + |
| 41 | +```kotlin |
| 42 | +cacheManager.add("HelloWorldKey", "Hello World", 60) |
| 43 | +cacheManager.add("IntValueKey", 255, 30) |
| 44 | +cacheManager.add("BooleanKey", false, 60) |
| 45 | +cacheManager.add("FloatKey", 5.55, 60) |
| 46 | +``` |
| 47 | + |
| 48 | +**To add element to cache instance without set lifetime:** |
| 49 | + |
| 50 | +```kotlin |
| 51 | +cacheManager.add("HelloWorldKey", "Hello World") |
| 52 | +cacheManager.add("IntValueKey", 255) |
| 53 | +cacheManager.add("BooleanKey", false) |
| 54 | +cacheManager.add("FloatKey", 5.55) |
| 55 | +``` |
| 56 | + |
| 57 | +## Getting element from cache: |
| 58 | +The `get` function accepts 3 parameters: `key: Stirng, checkExpired: Any, callback: (value: Any?, type: Class<*>?) -> Unit`. |
| 59 | + |
| 60 | +The checkExpired parameter is optional, default setted as true. |
| 61 | + |
| 62 | +In lambda callback `value` is a nullable any object and `type` is a nullable java Class. |
| 63 | + |
| 64 | +**To get element from cache instance with lifetime check:** |
| 65 | + |
| 66 | +```kotlin |
| 67 | +cacheManager.get("HelloWorldKey") { value, type -> |
| 68 | + System.out.println(value.toString()) |
| 69 | +} |
| 70 | + |
| 71 | +cacheManager.get("IntValueKey") { value, type -> |
| 72 | + System.out.println(value.toString()) |
| 73 | +} |
| 74 | + |
| 75 | +cacheManager.get("BooleanKey") { value, type -> |
| 76 | + System.out.println(value.toString()) |
| 77 | +} |
| 78 | + |
| 79 | +cacheManager.get("FloatKey") { value, type -> |
| 80 | + System.out.println(value.toString()) |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +**To get element from cache instance without lifetime check:** |
| 85 | + |
| 86 | +```kotlin |
| 87 | +cacheManager.get("HelloWorldKey", false) { value, type -> |
| 88 | + System.out.println(value.toString()) |
| 89 | +} |
| 90 | + |
| 91 | +cacheManager.get("IntValueKey", false) { value, type -> |
| 92 | + System.out.println(value.toString()) |
| 93 | +} |
| 94 | + |
| 95 | +cacheManager.get("BooleanKey", false) { value, type -> |
| 96 | + System.out.println(value.toString()) |
| 97 | +} |
| 98 | + |
| 99 | +cacheManager.get("FloatKey", false) { value, type -> |
| 100 | + System.out.println(value.toString()) |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Remove element from cache |
| 105 | +The `remove` function accept 1 parameter: `key: Stirng`. |
| 106 | + |
| 107 | + |
| 108 | +**To remove element from cache instance:** |
| 109 | +```kotlin |
| 110 | +cacheManager.remove("FloatKey") |
| 111 | +``` |
| 112 | + |
| 113 | +## Clear cache instance |
| 114 | +The `removeAllElements` function cleans the entire cache. |
| 115 | + |
| 116 | +**To clear cachce istance:** |
| 117 | + |
| 118 | +```kotlin |
| 119 | +cacheManager.removeAllElements() |
| 120 | +``` |
| 121 | + |
| 122 | +## List of the cahce files. |
| 123 | +The `getListOfCacheFiles` function accept 1 parameter: `ctx: Context` and return list of exist cache files. |
| 124 | + |
| 125 | +**To get cahce files:** |
| 126 | + |
| 127 | +```kotlin |
| 128 | +CacheManager.getListOfCacheFiles(appContext) |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +# Changelog |
| 134 | +Please see the [Changelog](https://github.com/pcpl2/CacheLib/wiki/Changelog) page to see what's recently changed. |
| 135 | + |
| 136 | + |
| 137 | +# License |
| 138 | +``` |
| 139 | +Copyright 2018 Patryk Ławicki |
| 140 | +
|
| 141 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 142 | +you may not use this file except in compliance with the License. |
| 143 | +You may obtain a copy of the License at |
| 144 | +
|
| 145 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 146 | +
|
| 147 | +Unless required by applicable law or agreed to in writing, software |
| 148 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 149 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 150 | +See the License for the specific language governing permissions and |
| 151 | +limitations under the License. |
| 152 | +``` |
0 commit comments