Android Tutorial [B4X] B4XCache - simple and useful cache collection

B4XCache is a new collection added in B4XCollections v1.10.
It is key / value store collection, similar to Map.
When the cache reaches the set maximum size, the least recent used items are removed (30% of the items).
The item recency is updated when it is added to the cache and whenever it is accessed, using Cache.Get.

You can also add "eternal" items using PutEternal. Those items will never be removed automatically and they also don't count when testing the maximum limit.

Dim cache As B4XCache
cache.MaxSize = 20
cache.Put("Image 1", bmp1)
cache.Put("Image 2", bmp2)
cache.Put("Image 3", bmp3)
cache.Put("Image 4", bmp4)
cache.PutEternal("Default Image", bmpDefault)

'Getting an item from the cache, don't assume that it is there:
Dim bmp As B4XBitmap
Dim Key As String = "Image 4"
If cache.ContainsKey(Key) Then
    bmp = cache.Get(Key)
    'Put also returns the Value.
    bmp = cache.Put(Key, XUI.LoadBitmap(...))
End If
Last edited:


Licensed User
Longtime User
I'm curious how you came up with this idea :)

I suggest having the user set the percentage of messages to be removed. Also, I would do a second version where, instead of relying on this percentage, items are deleted after N days (or I would implement this functionality in this same collection).

EDIT: "N days"? It's a collection, not a DB šŸ˜„


Well-Known Member
Licensed User
How do I get this for my own use? The B4XCollections thread says it is an internal library. Should I download the B4XLib anyway? or wait for a B4J update?


Well-Known Member
Licensed User
It's in the B4XCollections library. If it's not in yours then download B4A v11.2 Beta, as it is present in mine.
I'm using it from B4J. I have the 1.08 version of the lib, and it shows 1.10 online. I do have the latest B4J (9.30), so I guess I have to wait until the next B4J release to use the internal library, or can I use the downloadable one in the meantime?