The code below is not working.
mapView.getTileProvider().clearTileCache();
Is there another way to do it?
I found a solution to my question. After going through the source in order to find out why the cache is not cleared after executing
mapView.getTileProvider().clearTileCache();
i found two functions in:
org.osmdroid.tileprovider.modules.SqlTileWriter.java
Functions:
/* purges and deletes everything from the cache database */
public boolean purgeCache(){..}
and
/* purges and deletes all tiles from the given tile source name from the cache database */
public boolean purgeCache(String mTileSourceName){..}
Example to delete cache for specific tile source:
SqlTileWriter sqlTileWriter = new SqlTileWriter();
boolean isCleared = sqlTileWriter.purgeCache(mapView.getTileProvider().getTileSource().name());
Related
I am working on an app with Appcelerator.
Appcelerator uses nearly the same kind of require.js like node.js does.
Now I want to implement a feature that logges out the current user and does not leave any trace.
The most simple way would be to restart the app, but Appcelerator and especially Apple does not support this.
So i have to open the login window and clean all the data that leaves a trace to the old user.
The easiest way would be to dereference one of the main nodes in the require chain leaving all the data dereferenced and garbage collected.
I know there is a way (as mentioned here) to do that in node:
/**
* Removes a module from the cache
*/
function purgeCache(moduleName) {
// Traverse the cache looking for the files
// loaded by the specified module name
searchCache(moduleName, function (mod) {
delete require.cache[mod.id];
});
// Remove cached paths to the module.
// Thanks to #bentael for pointing this out.
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
if (cacheKey.indexOf(moduleName)>0) {
delete module.constructor._pathCache[cacheKey];
}
});
};
/**
* Traverses the cache to search for all the cached
* files of the specified module name
*/
function searchCache(moduleName, callback) {
// Resolve the module identified by the specified name
var mod = require.resolve(moduleName);
// Check if the module has been resolved and found within
// the cache
if (mod && ((mod = require.cache[mod]) !== undefined)) {
// Recursively go over the results
(function traverse(mod) {
// Go over each of the module's children and
// traverse them
mod.children.forEach(function (child) {
traverse(child);
});
// Call the specified callback providing the
// found cached module
callback(mod);
}(mod));
}
};
So I tried to read out the require-cache in Appcelerator with:console.log(require, "-" ,require.cache); with an output like: <KrollCallback: 0x79f6fe50> - <null>
So now my questions:
Is there a way to reach the require-cache in Appcelerator?
Do you know a way to clean up a big Appcelerator-App?
Since it is possible to wirte native Modules for Appcelerator:
Do you know a way to clean up a big Android App?
Do you know a way to clean up a big iOS App?
Thank you very much
We are trying to use the library on Android for TimedEviction. The items in the cache are expiring as soon as we overwrite an existing item.
We are building the cache as follows:
private Cache rssiMap;
RemovalListener removalListener = new RemovalListener() {
#Override
public void onRemoval(RemovalNotification removal) {
}
};
rssiMap = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.removalListener(removalListener)
.build();
rssiMap.put(device, rssi);
Is there something wrong we are doing with the code or is this a known issue?
This is correct behavior.
Actually, client code doesn't care WHEN the elements are expired, right? Client code does care about WHAT final cache values are.
The RemovalListener focuses on WHAT is evicted instead of WHEN.
JavaDoc of RemovalListener.onRemoval():
Notifies the listener that a removal occurred at some point in the
past.
By the way, I recommend use Cache.get() instead of Cache.put().
From JavaDoc of Cache.put():
Prefer get(Object, Callable) when using the conventional "if cached,
return; otherwise create, cache and return" pattern.
I'm attempting to draw an icon for an achievement using Google Play Game Services API.
However, it is silently failing.
Approach/Issue:
The URI for the image is retrieved successfully, exists and is valid.
I use ImageManager.loadImage to get the image, with an OnImageLoadedListener for callback (com.google.android.gms.common.images.ImageManager).
However OnImageLoadedListener's method, onImageLoaded, is never called.
No error's, no evidence, just completely ignored, I even waited 10 minutes just in case.
Code:
// Get URI [is valid, exists, is of type png, I checked]
Uri uri = getAchievementIconUri(id);
// Use ImageManager to get the icon image
ImageManager.create(context).
loadImage(new ImageManager.OnImageLoadedListener() {
#Override
public void onImageLoaded(Uri u, Drawable d, boolean r) {
/*
* This code is never reached, no call to onImageLoaded is made!
*/
}
}, uri);
It's probably a very late answer, but it might help other people experiencing the same. From the latest google documentation, https://developers.google.com/android/reference/com/google/android/gms/common/images/ImageManager.html#public-methods
Note that you should hold a reference to the listener provided until the callback is complete. For this reason, the use of anonymous implementations is discouraged.
This might explain the behaviour you experienced, as in the first code snippet provided, no reference to the Listener was retained.
Well, for what it matters, I never solved the problem entirely.
It seems that it actually displays the icons occasionally, whenever it feels like it.
I implemented a workaround that loads a compressed, cached version of the image and later replaces it with the ImageManager provided image if it can be bothered doing so.
If anybody works out the correct answer then I will remove this workaround, but until then its here for anybody with the same issue.
Code that loads the local icons:
public Bitmap loadDefaultAchievementIcon(final String id) {
// Get drawable ID for achievement
// iconIdMap is a map of achievement ID strings against their drawable res IDs
int resID = iconIdMap.containsKey(id) ? iconIdMap.get(id) :
// Fallback/unknown icon
R.drawable.ic_action_achievements;
// Load and return bitmap
InputStream is = getResources().openRawResource(resID);
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
I've run into this weird error, where some images get cached as usual and some don't, any idea why?
Both images do get displayed and memory cached just fine, but when offline some display error image.
For example, this works fine:
http://cs4381.vk.me/u73742951/a_58a41ac2.jpg
However, this does not: http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Android_robot.svg/220px-Android_robot.svg.png
Both work fine displaying and memcaching but the second doesn't get displayed from disk cache, although I think I see it being saved, as app says it has 12kB cache in the system settings
Edit
I checked out a clean copy of Volley and it does the same thing. Its definatelly a bug...
From what Ive found out its that images do get cached, but Bitmap cachedBitmap = mCache.getBitmap(cacheKey); always returns null, so the cache says it doesnt have the bitmaps and then proceedes to download it again, and fail when offline, weird
The reason you're not getting any hits is because the default behavior in Volley for disk caching is dependent on the HTTP headers of the element you're requesting (in your case, an image).
Check the volley logs and see if you get the "cache-hit-expired" message - that means that the image was cached but it's TTL is expired as far as the default disk cache is concerned.
If you want the default settings to work, the images must have a Cache-Control header like max-age=??? where the question marks indicate enough seconds from the time it was downloaded.
If you want to change the default behavior, I'm not sure, but I think you have to edit the code a bit.
Look at the CacheDispatcher class in the Volley source.
Hope that helps.
A quick and dirty way:
private static class NoExpireDiskBasedCache extends DiskBasedCache
{
public NoExpireDiskBasedCache(File rootDirectory, int maxCacheSizeInBytes)
{
super(rootDirectory, maxCacheSizeInBytes);
}
public NoExpireDiskBasedCache(File rootDirectory)
{
super(rootDirectory);
}
#Override
public synchronized void put(String key, Entry entry)
{
if (entry != null)
{
entry.etag = null;
entry.softTtl = Long.MAX_VALUE;
entry.ttl = Long.MAX_VALUE;
}
super.put(key, entry);
}
}
Which one is faster way to load mobile web pages and non mobile web pages in Android webview; loading cache or not loading that at all?
And what is recommend style to load that?
Right now when I don't load cache at all non mobile sites are much more slower to load than when I load them in native browser.
Don't use these:
viewer.getSettings().setAppCacheMaxSize(1024*1024*8);
viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
viewer.getSettings().setAppCacheEnabled(true);
These have nothing to do with the default webview internal cache. Appcache is an entirely different feature mean to make you able to run the website w/o an internet connection. It does not work that great and probably you do not want to use it.
With setting this: viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) is enough.
Of course, cached approach should be faster. That's the exact reason caching is there in the first place.
But you should be fine unless you specifically disable caching for webview. If you don't - it will use cache by default.
/*
public abstract void setAppCacheEnabled (boolean flag)
Sets whether the Application Caches API should be enabled. The default is false.
Note that in order for the Application Caches API to be enabled, a valid database
path must also be supplied to setAppCachePath(String).
Parameters
flag : true if the WebView should enable Application Caches
*/
// Enable the caching for web view
mWebView.getSettings().setAppCacheEnabled(true);
/*
public abstract void setAppCachePath (String appCachePath)
Sets the path to the Application Caches files. In order for the Application Caches
API to be enabled, this method must be called with a path to which the application
can write. This method should only be called once: repeated calls are ignored.
Parameters
appCachePath : a String path to the directory containing Application Caches files.
*/
/*
public abstract File getCacheDir ()
Returns the absolute path to the application specific cache directory on the
filesystem. These files will be ones that get deleted first when the device runs
low on storage. There is no guarantee when these files will be deleted.
Note: you should not rely on the system deleting these files for you; you should
always have a reasonable maximum, such as 1 MB, for the amount of space you consume
with cache files, and prune those files when exceeding that space.
The returned path may change over time if the calling app is moved to an adopted
storage device, so only relative paths should be persisted.
Apps require no extra permissions to read or write to the returned path,
since this path lives in their private storage.
Returns
The path of the directory holding application cache files.
*/
/*
public String getPath ()
Returns the path of this file.
*/
// Specify the app cache path
mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());
/*
public abstract void setCacheMode (int mode)
Overrides the way the cache is used. The way the cache is used is based on the
navigation type. For a normal page load, the cache is checked and content is
re-validated as needed. When navigating back, content is not re-validated, instead
the content is just retrieved from the cache. This method allows the client to
override this behavior by specifying one of
LOAD_DEFAULT,
LOAD_CACHE_ELSE_NETWORK,
LOAD_NO_CACHE or
LOAD_CACHE_ONLY.
The default value is LOAD_DEFAULT.
Parameters
mode : the mode to use
*/
/*
public static final int LOAD_DEFAULT
Default cache usage mode. If the navigation type doesn't impose any specific
behavior, use cached resources when they are available and not expired, otherwise
load resources from the network. Use with setCacheMode(int).
Constant Value: -1 (0xffffffff)
*/
/*
public static final int LOAD_CACHE_ELSE_NETWORK
Use cached resources when they are available, even if they have expired. Otherwise
load resources from the network. Use with setCacheMode(int).
Constant Value: 1 (0x00000001)
*/
/*
public static final int LOAD_NO_CACHE
Don't use the cache, load from the network. Use with setCacheMode(int).
Constant Value: 2 (0x00000002)
*/
/*
public static final int LOAD_CACHE_ONLY
Don't use the network, load from the cache. Use with setCacheMode(int).
Constant Value: 3 (0x00000003)
*/
// Set the cache mode
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);