Skip to content

Instantly share code, notes, and snippets.

@hakobera
Created January 14, 2012 08:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobera/1610744 to your computer and use it in GitHub Desktop.
Save hakobera/1610744 to your computer and use it in GitHub Desktop.
Titanium で ImageView のキャッシュを起動時に消す (for iOS only)
/**
* 指定した時間更新されていない画像キャッシュを削除する。
* iOS にのみ対応。
*
* @param {Number} expiredTime キャッシュ保持期限 (ms)
*/
module.exports = function(expiredTime) {
var cachePath = Ti.Filesystem.applicationDataDirectory + '../Library/Caches/',
cacheDir = Ti.Filesystem.getFile(cachePath),
now = new Date().getTime();
if (cacheDir) {
var files = cacheDir.getDirectoryListing();
if (files) {
for (var i = 0, l = files.length; i < l; ++i) {
var file = Ti.Filesystem.getFile(cachePath, files[i]),
filename = file.name;
// 画像キャッシュは '.' で終わるファイル名で保存されている
if (filename[filename.length - 1] === '.') {
if (now - file.modificationTimestamp() > expiredTime) {
Ti.API.debug(files[i]);
file.deleteFile();
}
}
}
}
}
};
// 7日間更新がない画像キャッシュファイルを削除する
require('deleteImageCache')(/*7days =*/7 * 24 * 60 * 60 * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment