Name: dualcache
Owner: Reddit
Description: This android library provide a cache with 2 layers, one in RAM in top of one disk.
Forked from: vincentbrison/dualcache
Created: 2017-05-02 23:04:15.0
Updated: 2017-12-24 15:23:26.0
Pushed: 2017-05-03 07:11:42.0
Homepage: null
Size: 607
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This android library provide a cache with 2 layers, one in RAM in top of one on local storage. This library is highly configurable :
| Configurations | Disk : Specific serializer
| Disk : disable
|
| ————– | ————————– | —————- |
| Ram : Specific serializer
| YES | YES |
| Ram : References
| YES | YES |
| Ram : disable
| YES | NO |
Specific serializer
: the object stored in cache will be serialized through a serializer provided by yourself.References
: the objects stored in Ram are cached through there references (no serialization is done).Disable
: the corresponding layer (Ram or disk) is disable.If you work with specific serializer
or references
you will have to provide (through an interface) the
way of compute the size of cached objects, to be able to correctly execute the LRU policy.
If you do not want to write your own serializer and a json serializer is enough for you, you can use
dualcache-jsonserializer
which will serialize object using Jackson
The following diagrams are showing how the dualcache
is working :
DualCache with specific serializer in RAM and specific serializer in disk.
DualCache with references in RAM and specific serializer in disk.
To get the best performance from this library, I recommend that you use larger size for the disk layer than for the Ram layer. When you try to get an object from the cache which is already in the Ram layer, the disk wont be use to keep the best performance from the Ram. If you try to get an object from the cache which is on disk and not on Ram, the object will be loaded into RAM, to ensure better further access time.
When you want to use a cache on Android today, you have two possibilities. You whether use :
The thing is the first one only works in RAM, and the second one only on disk (internal memory of the phone). So you need to choose whether if you will use the LruCache (RAM) :
Or you will use the DiskLruCache (Disk) :
The purpose of this library is to provide both features of these two caches, by making them working together. You do not need to ask yourself anymore “Should I use this one or this one ? But this one is persistent, but the other one is faster…“. With this library you only use one cache, with two layers, one in RAM, and one in Disk and you configure how they have to work to provide exactly what you need in term of caching for you application.
default
json serializer
is now a specific serializer which is available at com.vincentbrison.openlibraries.android:dualcache-jsonserializer:3.1.1
.Builder
.Starting with version 2.2.1
, the cache is supporting concurrent access. You can perform whatever operations from multiple threads and the cache
takes care of the synchronization. More than that, this synchronization is optimized to block the threads only if needed, to get the best performances.
In fact, put
and get
are synchronized on each entry, and the cache itself is locked trough a ReadWriteLock
for invalidation operations.
sitories {
nCentral()
oid {
agingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
dependencies {
compile 'com.vincentbrison.openlibraries.android:dualcache:3.1.1'
//compile 'com.vincentbrison.openlibraries.android:dualcache-jsonserializer:3.1.1' // If you
// want a ready to use json serializer
}
the configuration of the cache is done when you are building the cache through its `Builder` class.
c examples
==========
d your cache
-----------
st of all, you need to build you cache, through the `Builder` class.
A cache with a serializer for RAM and disk disable :
cache = new Builder<>(CACHE_NAME, TEST_APP_VERSION, AbstractVehicule.class)
.enableLog()
.useSerializerInRam(RAM_MAX_SIZE, new SerializerForTesting())
.noDisk()
.build();
A cache with references in RAM and a default serializer on disk :
cache = new Builder<>(CACHE_NAME, TEST_APP_VERSION, AbstractVehicule.class)
.enableLog()
.useReferenceInRam(RAM_MAX_SIZE, new SizeOfVehiculeForTesting())
.useSerializerInDisk(DISK_MAX_SIZE, true, new DualCacheTest.SerializerForTesting(), getContext())
.build();
can note that when you build the cache, you need to provide an `app version` number. When the cache
oaded, if data exist with a inferior number, it will be invalidate. It can be extremely useful when
update your app, and change your model, to avoid crashes. This feature is possible because the DiskLruCache of Jake Wharton
emented this feature.
ut an object into your cache, simply call `put` :
DummyClass object = new DummyClass(); object = cache.put(“mykey”, object);
et an object from your cache, simply call `get` :
DummyClass object = null; object = cache.get(“mykey”);
cases
=====
sing default serialization on RAM and on disk can be very useful for caching network exchange of data.
sing references in RAM and serialization on disk can be very useful to cache bitmaps.
doc
===
javadoc provided with this library is fully written and released on Maven.
ing
===
the configurations of the cache are (almost) fully tested through automated tests. If you fork
repo, you can launch them with the gradle command `connectedAndroidTest`.
need to have a device connected since the tests will be run on every device connected to your computer.
mulator or a [GenyMotion] instance is enough.
port will be available at : `/{location of your fork}/lib/build/outputs/reports/androidTests/connected/index.html`
nse
===
Copyright 2016 Vincent Brison.
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.