yangfuhai / asimplecache Goto Github PK
View Code? Open in Web Editor NEWa simple cache for android and java
License: Apache License 2.0
a simple cache for android and java
License: Apache License 2.0
https://github.com/xufeifandj/Acache 我看有个这个作者把
File f = new File(ctx.getCacheDir(), "ACache"); 改为了File f = new File(ctx.getFilesDir(), cacheName);
然后在说明里写的是“xff轻量级数据存储 修改来自 Michael Yang ,修改缓存路径,防止数据被清空”
用ctx.getFilesDir()是会让缓存的数据更安全一些吗?
如果想永久保存一个值,该如何传参?
目前支持这个功能吗, 好像源码里面没有看到哦!
调用cache.clear()方法后,不能重新创建ACache目录
I like how the project was created but did not understand why this:
public void put(String key, byte[] value) {
File file = mCache.newFile(key);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mCache.put(file);
}
}
When the api cache provides the ability to save in the cache based on File:
private void put(File file)
Faced with this situation, I suggest (what I currently need) that create a method that works with File(public):
public void put(File file)
So I could add a File in Cache, more record (when necessary), using a FileInputStream. Would look like this:
File mFile = new File("test.txt");
ACache mCache = ACache.get(this);
mCache.put("myFileKey", mFile);
FileOutputStream out = new FileOutputStream(mFile);
out.write("my bytes".getBytes());
The need then arose to write because I need to save content from the internet (save a cache of bytes in memory is not an option).
不同值,但是却同一个 hashCod)的,亲自验证过的
package com.sunkun.cache;
import java.io.File;
import javax.management.ImmutableDescriptor;
public class SimpleCacheTest {
public static void main(String args[])
{
SimpleCache mCache = SimpleCache.get(new File("").getAbsoluteFile(),10l,1000);
ImmutableDescriptor object = new ImmutableDescriptor("name=sunkun");
mCache.put("TestObject",object,1);
//测试超时
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!mCache.getAsObject("TestObject").equals(object))
System.out.println("failed");
else
System.out.println("suceed");
}
}
问题代码:
private void put(File file) {
int curCacheCount = cacheCount.get();
while (curCacheCount + 1 > countLimit) {
long freedSize = removeNext();
cacheSize.addAndGet(-freedSize);
curCacheCount = cacheCount.addAndGet(-1);
}
cacheCount.addAndGet(1);
long valueSize = calculateSize(file);
long curCacheSize = cacheSize.get();
while (curCacheSize + valueSize > sizeLimit) {
long freedSize = removeNext();
curCacheSize = cacheSize.addAndGet(-freedSize);
}
cacheSize.addAndGet(valueSize);
Long currentTime = System.currentTimeMillis();
file.setLastModified(currentTime);
lastUsageDates.put(file, currentTime);
}
由于软件版本升级,缓存的Json对象可能字段有变化。期望有简单判断版本的方法
`
//private static Map<String, ACache> mInstanceMap = Collections.synchronizedMap(new
HashMap<String, ACache>());
private static ACache instance = null;
/**
* 个人觉得没必要用map来缓存实例,一个就够用了
* @param cacheDir
* @param max_zise
* @param max_count
* @return
*/
public static ACache get(File cacheDir, long max_zise, int max_count) {
if (instance == null) {
synchronized (ACache.class) {
//ACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
if (instance == null) {
instance = new ACache(cacheDir, max_zise, max_count);
//mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
}
}
}
return instance;
}
/**
* hashCode发生碰撞怎么办,我建议用base64
* @param key
* @return
*/
private File newFile(String key) {
//return new File(cacheDir, key.hashCode() + "");
return new File(cacheDir, Base64.encodeToString(key.getBytes(), Base64.NO_WRAP) + "");
}
`
我改过的版本在这里:
https://github.com/moz1q1/WalleLibrary/tree/master/library_utils/src/main/java/org/wall/mo/utils/cache
你都不合并人家代码,我只能这样改了
例如在UI thread中初始化及存入值,在service或receiver中获取,由于pid不同,导致创建一个文件夹读取,抛出异常。是否可以指定以路径为主,忽略进程的方法呢?
例如查出来的结果是ArrayList resultList
我想作为缓存的话要怎么设置呢
[2013-10-10 18:29:46 - CloudTravel] W/ResourceType( 5088): Bad XML block: header size 798 or total size 0 is larger than data size 0
[2013-10-10 18:29:46 - CloudTravel] E:\WorkSpace\AndroidProject\ASimpleCache\AsimpleCacheDemo\res\menu\activity_save_jsonarray.xml:3: error: No resource identifier found for attribute 'showAsAction' in package 'android'
[2013-10-10 18:29:51 - CloudTravel] W/ResourceType( 4020): Bad XML block: header size 787 or total size 0 is larger than data size 0
[2013-10-10 18:29:51 - CloudTravel] E:\WorkSpace\AndroidProject\ASimpleCache\AsimpleCacheDemo\res\menu\activity_save_jsonarray.xml:3: error: No resource identifier found for attribute 'showAsAction' in package 'android'
mCache.put("test_key1", "test value");这个方法缓存多久?
private ACache(File cacheDir, long max_size, int max_count) {
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new RuntimeException("can't make dirs in "
+ cacheDir.getAbsolutePath());
}
}
主要是一段代码
昨天发现NumberFormatException这个异常,定位到判断时候过期的判断,但是缓存的数据并没有设置过期时间,但是刚好缓存的数据包含‘-’,查看那一段代码,是截取前13位字符,判断是不是时间的,然后转化为时间的,所以这里会有一个问题就是,极少数情况下,刚好string中包含,“-”,有同时满足后面有一个“ ”空格,就满足indexOf(data, mSeparator),这一个string就会被错认为一个包含过期时间的字段,然后转化时间就会报错,所以过期时间的判断有点不严谨,会有这种情况的出现,虽然很少很少,建议转化加一个try catch,可以解决
I am going to save a object array, How can ACache help me? Thank U?
怎么遍历自己缓存的数据
现在一般的应用都有统计缓存数据大小的功能,建议添加这个功能!
Is there any way to get expired caches and delete them?
可以查看文件的最近修改时间吗
My native language is Portuguese, do various translations of text from English to my language but Japanese / Chinese does not!
请问和sharedpreferenced相比,存和读取的性能咋样?
调用clear()缓存,无效,还是能获取缓存,是没权限,还是什么问题啊?
存String时,如果String的开头是类似UUID或者TimeStamp格式的东西时,取出过程报错。
目前均是disk cache,能否支持ram cache?
作者啊,这个项目是不是不在维护了。
Error:(78, 24) error: no suitable method found for get(FragmentActivity)
method ACache.get(String) is not applicable
(argument mismatch; FragmentActivity cannot be converted to String)
method ACache.get(File) is not applicable
(argument mismatch; FragmentActivity cannot be converted to File)
感觉还是把这个单独做成Library吧, 方便引用以及拉取更新!
例如 put(int id,List bean, .....)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.