Code Monkey home page Code Monkey logo

cachelib's Introduction

SimpleCache

Build Status SimpleCache API License

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.1.0'
}

Basic usage

To init instance of cache manager without filename:

val cacheManager = CacheManager.createInstance(appContext, null) 

The cacheManager instance usage default cache file.

To init instance of cache manager with filename:

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

The cacheManager instance usage cache with file name filesCache.

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)

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()

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)

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

Watchers

 avatar

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.