B4A Library [Lib] Cache

This library adds a LRU cache to your application. It is based upon the memory cache introduced in API 12 (Honeycomb) and the disk cache introduced in API 14 (ICS). The library is thread-safe and works with Android versions > 1. The memory cache accepts only bitmaps. The disk cache accepts serializable objects (strings, numbers, lists, arrays...) and bitmaps.

If you're wondering what's a cache and if you need it, here's the Wikipedia definition:
[...] A cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache (cache hit), this request can be served by simply reading the cache, which is comparatively faster. Otherwise (cache miss), the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes. [...]

This cache uses the LRU (Least Recently Used) algorithm. Each time a value is stored or accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

A cache may be useful in various situations. Its common use is for downloaded resources or image galleries, but you can also use it in a state manager, or in a game to improve performance.
The demo should prove you the benefit of using a cache.

List of properties and methods

If you want to put the disk cache on an external storage media, don't forget to add the following permission with the Manifest editor:
B4X:
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")

v1.1:
I added statistics for the memory cache (miss count, hit count, etc.)
I added the IsClosed function for the disk cache.

v1.11:
Fixed a minor bug.

v1.34:
I improved some functions and fixed minor bugs.
I added two properties: FreeMemory and MaxMemory.

v1.35:
I improved the synchronization of asynchronous operations.

v1.36:
I fixed a rare bug with very large caches.
I made an internal change for UltimateListView.
I updated the deprecated links of the demo.

v1.37:
I fixed a bug with truncated lines.
I ignore any error resulting from a call to StatFs because this OS library does not work properly on some devices. I use a default value when an error occurs.
The key for the disk cache accepts longer names.

v1.38:
I fixed a bug with the default cache folder (this concerns mainly the ULV users).
I also changed the GetBitmap function so the message about the source is sent to the unfiltered log now, instead of the filtered log.
 

Attachments

  • Cache v1.38.zip
    74.3 KB · Views: 902
  • SourceJava_Cache_1_38.zip
    51.1 KB · Views: 452
Last edited:
D

Deleted member 30048

Guest
Are not supposed to everytime I rotate the phone all variables are reset except the variables of process Globals?
 

fotosettore

Member
Licensed User
Longtime User
hi !
using the code
B4X:
Dim l As List
l = Cache.DiskJournal
For i = 0 To l.Size - 1
   Log(l.Get(i))
Next
b4a stop in l = Cache.DiskJournal and return this error ---> java.lang.NullPointerException

the same problem with all methods
i'm using a rooted phone and android 4.4.3

many thanks for help
 

fotosettore

Member
Licensed User
Longtime User
Initialize the class before using it.
many thanks - stupid question before from me !!! now it works fine.
another question :
is it possibile use your library to delete phone system cache ?
in other words i'd like to automatize these steps :
SETTINGS->STORAGE->INTERNAL STORAGE->CACHED ----> clear
SETTINGS->STORAGE->PHONE STORAGE->CACHED -----> clear
 

Informatix

Expert
Licensed User
Longtime User
many thanks - stupid question before from me !!! now it works fine.
another question :
is it possibile use your library to delete phone system cache ?
in other words i'd like to automatize these steps :
SETTINGS->STORAGE->INTERNAL STORAGE->CACHED ----> clear
SETTINGS->STORAGE->PHONE STORAGE->CACHED -----> clear
Unless your device is rooted, you cannot access or modify the data in another internal directory.
 

Marco Nissen

Active Member
Licensed User
Longtime User
Unless your device is rooted, you cannot access or modify the data in another internal directory.

With B4A Beta4 I get an exception during initialization of Cache .. can you fix that please? Thank you


B4X:
java.lang.IllegalArgumentException: Invalid path:
   at android.os.StatFs.doStat(StatFs.java:46)
   at android.os.StatFs.<init>(StatFs.java:39)
   at flm.b4a.cache.Cache.Initialize(SourceFile:77)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
   at com.wurzelkraut.Stash2Go.main.afterFirstLayout(main.java:98)
   at com.wurzelkraut.Stash2Go.main.access$100(main.java:16)
   at com.wurzelkraut.Stash2Go.main$WaitForLayout.run(main.java:76)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5221)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.system.ErrnoException: statvfs failed: ENOENT (No such file or directory)
   at libcore.io.Posix.statvfs(Native Method)
   at libcore.io.BlockGuardOs.statvfs(BlockGuardOs.java:298)
   at android.system.Os.statvfs(Os.java:473)
   at android.os.StatFs.doStat(StatFs.java:44)
   ... 21 more
 

Marco Nissen

Active Member
Licensed User
Longtime User
Well, it works totally fine in 3.82

the call is Cache.Initialize(MemorySize, DiskSize, "")
no path given ..
 
Top