成都网站建设设计

将想法与焦点和您一起共享

使用缓存问题-创新互联

public class CacheManager {

   private static final long MAX_SIZE = 5242880L; // 5MB

   private CacheManager() {

   }

   public static void cacheData(Context context, byte[] data, String name) throws IOException {

       File cacheDir = context.getCacheDir();
       long size = getDirSize(cacheDir);
       long newSize = data.length + size;

       if (newSize > MAX_SIZE) {
           cleanDir(cacheDir, newSize - MAX_SIZE);
       }

       File file = new File(cacheDir, name);
       FileOutputStream os = new FileOutputStream(file);
       try {
           os.write(data);
       }
       finally {
           os.flush();
           os.close();
       }
   }

   public static byte[] retrieveData(Context context, String name) throws IOException {

       File cacheDir = context.getCacheDir();
       File file = new File(cacheDir, name);

       if (!file.exists()) {
           // Data doesn't exist
           return null;
       }

       byte[] data = new byte[(int) file.length()];
       FileInputStream is = new FileInputStream(file);
       try {
           is.read(data);
       }
       finally {
           is.close();
       }

       return data;
   }

   private static void cleanDir(File dir, long bytes) {

       long bytesDeleted = 0;
       File[] files = dir.listFiles();

       for (File file : files) {
           bytesDeleted += file.length();
           file.delete();

           if (bytesDeleted >= bytes) {
               break;
           }
       }
   }

   private static long getDirSize(File dir) {

       long size = 0;
       File[] files = dir.listFiles();

       for (File file : files) {
           if (file.isFile()) {
               size += file.length();
           }
       }

       return size;
   }
}

创新互联是一家集网站建设,岭东企业网站建设,岭东品牌网站建设,网站定制,岭东网站建设报价,网络营销,网络优化,岭东网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:使用缓存问题-创新互联
网页路径:http://chengdu.cdxwcx.cn/article/dcpccc.html