Cache
Cache
Events:
- CopyDone (Key As String, Error As Boolean)
- GetDone (Key As String, Obj As Object)
- PutDone (Key As String, Error As Boolean)
Methods:
- BitmapSize (bitmap As Bitmap) As Long
Returns the size in bytes of the given bitmap.
- ClearDiskCache
Deletes all files in the cache directory including files that weren't created by the cache.
- ClearMemoryCache
Clears the memory cache holding bitmaps.
- CloseAndClearDiskCache
Closes the disk cache and deletes all files in the cache directory including files that weren't created by the cache.
- CloseDiskCache
Closes the disk cache. Stored values will remain on the filesystem.
- CopyFileToDisk (key As String, dir As String, filename As String)
Copies a file in the disk cache.
- CopyFileToDisk_Async (key As String, dir As String, filename As String, eventPrefix As String)
Copies a file in the disk cache (asynchronously).
eventPrefix = prefix of the "CopyDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_CopyDone(key As String, Error As Boolean)
Error is true if there was an error.
- DiskFree As Long
Returns the available space in bytes of the disk cache.
- DiskJournal As List
Returns a copy of the journal of the disk cache.
Example:
Dim l As List
l = myCache.DiskJournal
For i = 0 To l.Size - 1
Log(l.Get(i))
Next
- DiskList As Map
Returns a list of the current contents of the disk cache, unordered.
Map structure: key=filename, value=object array (0=time of the last modification, 1=size in bytes)
Example:
Dim m As Map
m = myCache.DiskList
Dim Info(2) As Object
For i = 0 To m.Size - 1
Log(m.GetKeyAt(i))
Info = m.GetValueAt(i)
Log(" " & DateTime.Date(Info(0)) & " " & DateTime.Time(Info(0)))
Log(" " & Info(1) & " bytes")
Next
- DiskUsed As Long
Returns the sum of the sizes in bytes of the files in the disk cache.
- GetBitmap (key As String, updateMemory As Boolean) As Bitmap
Returns the bitmap for key from one of the caches (the memory cache is looked first).
Returns null if the key was not found.
The bitmap source (memory or disk cache) is specified in the unfiltered log.
updateMemory = if true, the memory cache is updated if the bitmap was only on disk.
- GetBitmapFromDisk (key As String, colorReduction As Boolean) As Bitmap
Returns the bitmap for key from the disk cache.
Returns null if the key was not found.
colorReduction = if True, the number of colors is reduced to save memory. This setting has no effect if some pixels are not opaque.
- GetBitmapFromDisk_Async (key As String, colorReduction As Boolean, eventPrefix As String)
Returns the bitmap for key from the disk cache (asynchronously).
colorReduction = if True, the number of colors is reduced to save memory. This setting has no effect if some pixels are not opaque.
eventPrefix = prefix of the "GetDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_GetDone(key As String, bmp As Bitmap)
bmp is null if the key was not found or there was an error.
- GetBitmapFromMemory (key As String) As Bitmap
Returns the Bitmap for key if it exists in the cache.
Returns null if not found.
- GetBitmap_Async (key As String, updateMemory As Boolean, eventPrefix As String)
Returns the given bitmap for key from one of the caches.
If the bitmap is in memory, it is sent back immediately. Otherwise, it is loaded from disk asynchronously.
updateMemory = if true, the memory cache is updated if the bitmap was only found on disk.
eventPrefix = prefix of the "GetDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_GetDone(key As String, bmp As Bitmap)
bmp is null if the key was not found or there was an error.
- GetDirectory As String
Returns the directory where the disk cache stores its data.
- GetObjectFromDisk (key As String) As Object
Returns the object for key from the disk cache.
Returns null if the key was not found.
- GetObjectFromDisk_Async (key As String, eventPrefix As String)
Returns the object for key from the disk cache (asynchronously).
eventPrefix = prefix of the "GetDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_GetDone(key As String, obj As Object)
obj is null if the key was not found or there was an error.
- GetStringFromDisk (key As String) As String
Gets the string for key from the disk cache.
Returns an empty string if the key was not found.
- Initialize (memoryCachePct As Byte, diskCacheSize As Long, diskCacheDir As String)
Initializes the caches.
memoryCachePct: percentage of free memory to use for the memory cache (this cache is only used for bitmaps, the other objects are stored on disk). Cannot exceed 90.
diskCacheSize: size in bytes of the cache on disk.
diskCacheDir: folder where the cache files are stored.
If empty, the default cache folder is used. This folder is specific to the application.
Example: myCache.Initialize(25, 20 * 1024 * 1024, "/mnt/sdcard/myAppCache")
'25% for the memory cache and 20MB for the disk cache
- IsClosed As Boolean
Returns true if the disk cache has been closed.
- IsInMemory (key As String) As Boolean
Returns true if the Bitmap for key is still in the memory cache.
- IsOnDisk (key As String) As Boolean
Returns true if the file for key is still in the disk cache.
- MemoryEvictionCount As Int
Returns the number of bitmaps that have been evicted from the memory cache.
- MemoryFree As Int
Returns the available space in bytes of the memory cache.
- MemoryHitCount As Int
Returns the number of times GetBitmapFromMemory returned a bitmap that was present in the cache.
- MemoryList As Map
Returns a copy of the current contents of the memory cache, ordered from least recently accessed to most recently accessed.
- MemoryMissCount As Int
Returns the number of times GetBitmapFromMemory returned null (bitmap not found).
- MemoryPutCount As Int
Returns the number of times PutBitmapInMemory was called.
- MemoryUsed As Int
Returns the sum of the sizes in bytes of the entries in the memory cache.
- PutBitmapInMemory (key As String, bitmap As Bitmap) As Boolean
Caches the given bitmap in memory.
Returns false if the bitmap is too big for the cache.
- PutBitmapOnDisk (key As String, bitmap As Bitmap, format As String, quality As Int)
Stores the given bitmap in the disk cache.
Format: "JPEG" or "PNG"
Quality: quality of JPEG compression (0..100). This setting is ignored for the PNG format.
- PutBitmapOnDisk_Async (key As String, bitmap As Bitmap, format As String, quality As Int, eventPrefix As String)
Stores the given bitmap in the disk cache (asynchronously).
Format: "JPEG" or "PNG"
Quality: quality of JPEG compression (0..100). This setting is ignored for the PNG format.
eventPrefix = prefix of the "PutDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_PutDone(key As String, Error As Boolean)
Error is true if there was an error.
- PutObjectOnDisk (key As String, obj As Object)
Stores the given object in the disk cache.
This works only with serializable objects (numbers, booleans, lists, arrays...).
- PutObjectOnDisk_Async (key As String, obj As Object, eventPrefix As String)
Stores the given object in the disk cache (asynchronously).
This works only with serializable objects (numbers, booleans, lists, arrays...).
eventPrefix = prefix of the "PutDone" event.
An event is triggered when the job ends. You can get it with:
Sub eventPrefix_PutDone(key As String, Error As Boolean)
Error is true if there was an error.
- PutStringOnDisk (key As String, value As String)
Stores the given string in the disk cache.
- RemoveBitmapFromMemory (key As String)
Removes the Bitmap for key if it exists.
- RemoveFromDisk (key As String) As Boolean
Removes the file for key if it exists in the disk cache.
Returns true if the file was removed.
Properties:
- BufferSize As Int
Gets/sets the buffer size of the disk cache. By default, it is set to the block size of the filesystem.
- FreeMemory As Long [read only]
Gets an approximation of the amount (in bytes) of free memory available to the running program.
- MaxMemory As Long [read only]
Gets the maximum amount of memory (in bytes) that may be used by the running program.
Author: F. Leneuf-Magaud/Informatix