Code Monkey home page Code Monkey logo

cachelib's Introduction

SimpleCache

Build Status SimpleCache License API Android Arsenal

A simple library for saving data in the cache and reading them.

Setup

The library is hosted on jcenter. To use it, add the following to your module level build.gradle file's dependencies:

dependencies {
    implementation 'com.github.pcpl2:simplecache:1.2.0'
}

Basic usage

To init instance of cache manager without filename:

val cacheManager = CacheManager.createInstance(context = appContext)

The cacheManager instance usage default cache file.

To init instance of cache manager with filename:

val cacheManager = CacheManager.createInstance(context = appContext, fileName = "filesCache")

The cacheManager instance usage cache with file name filesCache.

To init instance of cache manager without autosave:

val cacheManager = CacheManager.createInstance(context = appContext, autoSave = false)

If you have deactivated the automatic saving, you must remember to save the data using the save method.

Add or update elements to cache:

The set function accepts 3 parameters: key: Stirng, value: Any, lifetime: Long.

The lifetime parameter is the lifetime in seconds and is optional, default set as 0 (no lifetime).

To add or update element to cache instance with set lifetime:

cacheManager.set("HelloWorldKey", "Hello World", 60)
cacheManager.set("IntValueKey", 255, 30)
cacheManager.set("BooleanKey", false, 60)
cacheManager.set("FloatKey", 5.55, 60)

To add or update element to cache instance without set lifetime:

cacheManager.set("HelloWorldKey", "Hello World")
cacheManager.set("IntValueKey", 255)
cacheManager.set("BooleanKey", false)
cacheManager.set("FloatKey", 5.55)

Save cache data

To save cache on disk (if autosave disabled):

cacheManager.save()

Getting element from cache:

The get function accepts 4 parameters: key: Stirng, checkExpired: Boolean = true, success: (value: Any, type: Class<*>) -> Unit, error: (() -> Unit)? = null.

The checkExpired parameter is optional, default set as true.

Callback success return value from map and value type.

Callback error is optional and run if element with key do not exist or lifetime of element is ended.

To get element from cache instance with lifetime check:

cacheManager.get(key = "HelloWorldKey", success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
})

cacheManager.get(key = "IntValueKey", success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
})

cacheManager.get(key = "BooleanKey", success = { value, type ->
   Log.d("simpleCacheLog", value.toString())
}, error = { 
    Log.d("simpleCacheLog", "BooleanKey is empty.")
})

cacheManager.get(key = "FloatKey", success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
}, error = { 
    Log.d("simpleCacheLog", "FloatKey is empty.")
})

To get element from cache instance without lifetime check:

cacheManager.get(key = "HelloWorldKey", checkExpired = false, success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
})

cacheManager.get(key = "IntValueKey", checkExpired = false, success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
})

cacheManager.get(key = "BooleanKey", checkExpired = false, success = { value, type ->
   Log.d("simpleCacheLog", value.toString())
}, error = { 
    Log.d("simpleCacheLog", "BooleanKey is empty.")
})

cacheManager.get(key = "FloatKey", checkExpired = false, success = { value, type ->
    Log.d("simpleCacheLog", value.toString())
}, error = { 
    Log.d("simpleCacheLog", "FloatKey is empty.")
})

Remove element from cache

The remove function accept 1 parameter: key: Stirng.

To remove element from cache instance:

cacheManager.remove("FloatKey")

Clear cache instance

The removeAllElements function cleans the entire cache.

To clear cache istance:

cacheManager.removeAllElements()

To remove all elements in cache istance:

cacheManager.removeAllElements()

The dispose function accept 1 parameter: save: Boolean = true.

The save parameter is optional, default set as true.

List of the cache files.

The getListOfCacheFiles function accept 1 parameter: ctx: Context and return list of exist cache files.

To get cache files:

CacheManager.getListOfCacheFiles(appContext)

Global instances

Create global instance

The createGlobalInstance function accept 4 parameters: context: Context, instanceName: String, fileName: String = "CacheBase", autoSave: Boolean = true and return cache instance.

To create global instance:

CacheManager.createGlobalInstance(context = appContext, instanceName = "Instance1", fileName = "instanceFile1")

Get global instance

The getGlobalInstance function accept 1 parameter: instanceName: String and return cache instance.

To get global instance by name:

val globalInstance2 = CacheManager.getGlobalInstance(instanceName = "Instance1")

To get list of global instance names:

var listOfIInstancesNames = CacheManager.getListOfGlobalInstanceNames()

Remove global instance

The removeGlobalInstance function accept 2 parameters: instanceName: String, save: Boolean = true.

The save parameter is optional, default set as true.

To remove global instance by name:

CacheManager.removeGlobalInstance(instanceName = "Instance1")

To remove all global instances:

CacheManager.removeAllGlobalInstances()

Changelog

Please see the Changelog page to see what's recently changed.

License

Copyright 2018 Patryk Ławicki

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

cachelib's People

Contributors

pcpl2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cachelib's Issues

ClassNotFoundException for Gson and Joda

According to the project's build file, gson and joda-time is already included.

However, if I only include CacheLib, I get the error below

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.gson.GsonBuilder" on path: DexPathList[[zip file "/data/app/foo.bar.baz-G4Zk4FIQSWiDXJEaqDt4mQ==/base.apk"],nativeLibraryDirectories=[/data/app/foo.bar.baz-G4Zk4FIQSWiDXJEaqDt4mQ==/lib/arm, /system/lib, /vendor/lib]]

However, I include the libraries manually and it works:

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'joda-time:joda-time:2.10.2'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.