hdknr / note

Markdown Texts
http://qiita.com/hidelafoglia
7 stars 1 forks source link

FFImageLoading : Caching #4

Open hdknr opened 8 years ago

hdknr commented 8 years ago

FFImageLoading.Shared/Cache/DownloadCache.cs

        public async Task<CacheStream> GetStreamAsync(string url, CancellationToken token, TimeSpan? duration = null, string key = null)
        {
            /// keyがないのでURLのMD5

            string filename = string.IsNullOrWhiteSpace(key) ? _md5Helper.MD5(url) : _md5Helper.MD5(key);
            string basePath = await _diskCache.GetBasePathAsync().ConfigureAwait(false);
            string filepath = basePath == null ? filename : Path.Combine(basePath, filename);
            var diskStream = await _diskCache.TryGetStreamAsync(filename).ConfigureAwait(false);
            if (diskStream != null)
                return new CacheStream(diskStream, true);

            var memoryStream = await DownloadStreamAndCacheAsync(url, filename, filepath, token, duration).ConfigureAwait(false);
            return new CacheStream(memoryStream, false);
        }

FFImageLoading.Shared/Cache/DiskCache.cs

        public async Task<Stream> TryGetStreamAsync(string key)
        {
            // key  "b3415227e41fe379a31986f2bd8dd517"  
            key = SanitizeKey(key);

            await WaitForPendingWriteIfExists(key).ConfigureAwait(false);

            if (!entries.ContainsKey(key))
                return null;

            // basePath "/data/data/com.c1rus.ImageLoading.Sample/cache/ImageService"           try
            {
                string filepath = Path.Combine(basePath, key);
                return FileStore.GetInputStream(filepath);
            }
            catch
            {
                return null;
            }   
        }
hdknr commented 8 years ago
$ ls -al
total 168
drwxr-xr-x+ 4 hide  staff    136  2 22 17:33 .
drwxr-xr-x+ 3 hide  staff    102  2 22 17:08 ..
-rw-r--r--+ 1 hide  staff    888  2 22 17:33 .journal
-rw-r--r--+ 1 hide  staff  81189  2 22 17:33 96ebb8e99a9eed11be91491adf8de2ba
$ more .journal 
<U+FEFF>MONOID
1.0
c 96ebb8e99a9eed11be91491adf8de2ba 635917253065387720 1000
c 96ebb8e99a9eed11be91491adf8de2ba 635917253623538660 1000
c 96ebb8e99a9eed11be91491adf8de2ba 635917254081805970 1000
c 96ebb8e99a9eed11be91491adf8de2ba 635917256493376460 1000
c 96ebb8e99a9eed11be91491adf8de2ba 635917260143744540 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917260476812730 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917260668045900 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917260944484040 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917261325326790 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917262404934230 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917263039861720 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917263445101880 2592000000
c 96ebb8e99a9eed11be91491adf8de2ba 635917264499346070 1000
c 96ebb8e99a9eed11be91491adf8de2ba 635917268191302560 1000