Giter Site home page Giter Site logo

asimplecache's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asimplecache's Issues

把ASimpleCache拉下来然后作为Library引用结果报错!

[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'

多进程调用时异常

例如在UI thread中初始化及存入值,在service或receiver中获取,由于pid不同,导致创建一个文件夹读取,抛出异常。是否可以指定以路径为主,忽略进程的方法呢?

二级缓存

目前均是disk cache,能否支持ram cache?

clear 无效?

调用clear()缓存,无效,还是能获取缓存,是没权限,还是什么问题啊?

极少数情况下会出现NumberFormatException

昨天发现NumberFormatException这个异常,定位到判断时候过期的判断,但是缓存的数据并没有设置过期时间,但是刚好缓存的数据包含‘-’,查看那一段代码,是截取前13位字符,判断是不是时间的,然后转化为时间的,所以这里会有一个问题就是,极少数情况下,刚好string中包含,“-”,有同时满足后面有一个“ ”空格,就满足indexOf(data, mSeparator),这一个string就会被错认为一个包含过期时间的字段,然后转化时间就会报错,所以过期时间的判断有点不严谨,会有这种情况的出现,虽然很少很少,建议转化加一个try catch,可以解决

缓存对象长度大于缓存最大长度会导致死循环

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);
        }

如何永久保存一个值?

如果想永久保存一个值,该如何传参?
目前支持这个功能吗, 好像源码里面没有看到哦!

no suitable method found for FragmentActivity ?

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)

Suggestion for the project.

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).

代码优化建议

`

//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
你都不合并人家代码,我只能这样改了

Expired items

Is there any way to get expired caches and delete them?

建议

感觉还是把这个单独做成Library吧, 方便引用以及拉取更新!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.