├── .gitignore ├── .idea ├── encodings.xml ├── libraries │ ├── Dart_Packages.xml │ ├── Dart_SDK.xml │ └── Flutter_Plugins.xml ├── misc.xml ├── modules.xml ├── vcs.xml └── workspace.xml ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── dev └── bots │ ├── travis_install.sh │ └── travis_script.sh ├── disk_lru_cache.iml ├── example ├── .gitignore ├── .idea │ ├── codeStyles │ │ └── Project.xml │ ├── libraries │ │ ├── Dart_Packages.xml │ │ ├── Dart_SDK.xml │ │ ├── Flutter_Plugins.xml │ │ └── Flutter_for_Android.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations │ │ └── main_dart.xml │ └── workspace.xml ├── .metadata ├── README.md ├── android │ ├── .gitignore │ ├── .idea │ │ ├── codeStyles │ │ │ └── Project.xml │ │ ├── gradle.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ └── runConfigurations.xml │ ├── app │ │ ├── build.gradle │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── example │ │ │ │ └── MainActivity.java │ │ │ └── res │ │ │ ├── drawable │ │ │ └── launch_background.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ └── values │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── example.iml ├── example_android.iml ├── ios │ ├── .gitignore │ ├── Flutter │ │ ├── AppFrameworkInfo.plist │ │ ├── Debug.xcconfig │ │ ├── Release.xcconfig │ │ └── flutter_export_environment.sh │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ └── contents.xcworkspacedata │ └── Runner │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ ├── Icon-App-20x20@1x.png │ │ │ ├── Icon-App-20x20@2x.png │ │ │ ├── Icon-App-20x20@3x.png │ │ │ ├── Icon-App-29x29@1x.png │ │ │ ├── Icon-App-29x29@2x.png │ │ │ ├── Icon-App-29x29@3x.png │ │ │ ├── Icon-App-40x40@1x.png │ │ │ ├── Icon-App-40x40@2x.png │ │ │ ├── Icon-App-40x40@3x.png │ │ │ ├── Icon-App-60x60@2x.png │ │ │ ├── Icon-App-60x60@3x.png │ │ │ ├── Icon-App-76x76@1x.png │ │ │ ├── Icon-App-76x76@2x.png │ │ │ └── Icon-App-83.5x83.5@2x.png │ │ └── LaunchImage.imageset │ │ │ ├── Contents.json │ │ │ ├── LaunchImage.png │ │ │ ├── LaunchImage@2x.png │ │ │ ├── LaunchImage@3x.png │ │ │ └── README.md │ │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ │ ├── Info.plist │ │ └── main.m ├── lib │ ├── main.dart │ └── test_disk_cache.dart ├── pubspec.lock ├── pubspec.yaml └── test │ └── widget_test.dart ├── flutter_01.log ├── lib ├── _src │ ├── disk_lru_cache.dart │ ├── ioutil.dart │ └── lru_map.dart └── disk_lru_cache.dart ├── pubspec.lock ├── pubspec.yaml └── test ├── disk_lru_cache_test.dart └── lru_map_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | 4 | .packages 5 | .pub/ 6 | 7 | build/ 8 | ios/.generated/ 9 | ios/Flutter/Generated.xcconfig 10 | ios/Runner/GeneratedPluginRegistrant.* 11 | coverage/lcov.info 12 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/libraries/Dart_Packages.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | -------------------------------------------------------------------------------- /.idea/libraries/Dart_SDK.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/libraries/Flutter_Plugins.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 37 | 38 | 39 | 41 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: dart 2 | env: 3 | - SHARD=dartfmt 4 | - SHARD=test 5 | install: 6 | - ./dev/bots/travis_install.sh 7 | script: 8 | - ./dev/bots/travis_script.sh 9 | after_success: 10 | - coveralls-lcov coverage/lcov.info -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # [0.0.2] 2 | 3 | * Use synchronized library. 4 | 5 | 6 | ## [0.0.1] - Basic usage. 7 | 8 | * LRU Map 9 | * Store One `CacheEntry` with multiple files by using `CacheEditor` 10 | * Get `CacheSnapshot` and open streams directly from `CacheEntry` 11 | * Using a key to remove from cache 12 | * A record file to store all operation info,including 'DIRTY','CLEAN','REMOVE','READ' 13 | * Get total size from cache 14 | * When cache.size > caches.maxSize , auto trim to size. 15 | 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Xueliang Ren 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |

3 | 4 | Build Status 5 | 6 | 7 | Coverage Status 8 | 9 | 10 | PRs Welcome 11 | 12 | 13 | pub package 14 | 15 |

16 | 17 | 18 | 19 | # disk_lru_cache 20 | Disk lru cache for flutter. [wiki](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)) 21 | 22 | A cache that uses a bounded amount of space on a filesystem. 23 | Each cache entry has a string key and a fixed number of files, witch is accessible as stream. 24 | 25 | ## Use cases 26 | 27 | 28 | #### Working with memery 29 | 30 | We provided a `LruMap` ,in order to support LRU order in memory, witch is a subclass of Map.So ,wo can use the `LruMap` just like Map 31 | 32 | 33 | 34 | ``` 35 | final LruMap map = new LruMap(); 36 | 37 | expect(map.values.toList().length, 0); 38 | 39 | map['a'] = 1; 40 | map['b'] = 2; 41 | map['c'] = 3; 42 | 43 | /// use the key 'a' 44 | var f = map['a']; 45 | 46 | /// We use the key 'a', so at this moment it is the last element. 47 | alues = map.values; 48 | expect(values.toList()[0], 2); 49 | expect(values.toList()[1], 3); 50 | expect(values.toList()[2], 1); 51 | 52 | ``` 53 | 54 | 55 | #### Working with file system 56 | 57 | The basic usage is like this: 58 | 59 | 60 | With string: 61 | 62 | ``` 63 | int maxSize = 64 | 10 * 1024 * 1024; // 10M 65 | 66 | // Make sure it's writable 67 | Directory cacheDirectory = 68 | new Directory("${Directory.systemTemp.path}/cache"); 69 | 70 | DiskLruCache cache = new DiskLruCache( 71 | maxSize: maxSize, directory: cacheDirectory, filesCount: 1); 72 | 73 | // write stream 74 | CacheEditor editor = await cache.edit('filekey'); 75 | if(editor!=null){ 76 | IOSink sink = await editor.newSink(0); 77 | sink.write('your value'); 78 | await sink.close(); 79 | await editor.commit(); 80 | } 81 | 82 | // read stream 83 | CacheSnapshot snapshot = await cache.get('filekey'); 84 | String str = await snapshot.getString(0); 85 | print(str); 86 | 87 | ``` 88 | 89 | 90 | With bytes 91 | 92 | ``` 93 | // write bytes 94 | CacheEditor editor = await cache.edit('imagekey'); 95 | if(editor!=null){ 96 | HttpClient client = new HttpClient(); 97 | HttpClientRequest request = await client.openUrl("GET", Uri.parse("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1534075481&di=1a90bd266d62bc5edfe1ce84ac38330e&src=http://photocdn.sohu.com/20130517/Img376200804.jpg")); 98 | HttpClientResponse response = await request.close(); 99 | Stream> stream = await editor.copyStream(0, response); 100 | // The bytes has been written to disk at this point. 101 | await new ByteStream(stream).toBytes(); 102 | await editor.commit(); 103 | 104 | // read stream 105 | CacheSnapshot snapshot = await cache.get('imagekey'); 106 | Uint8List bytes = await snapshot.getBytes(0); 107 | print(bytes); 108 | } 109 | 110 | ``` 111 | 112 | 113 | ## Manage the cache 114 | 115 | 116 | #### Get the bytes of the cache in file system 117 | 118 | ``` 119 | DiskLruCache cache = ...; 120 | print(cache.size) 121 | ``` 122 | 123 | #### Clean the cache 124 | 125 | ``` 126 | DiskLruCache cache = ...; 127 | cache.clean(); 128 | 129 | ``` 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /dev/bots/travis_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "$PWD" 4 | export ROOT="$PWD" 5 | 6 | mkdir ~/development 7 | 8 | cd ~/development 9 | wget https://storage.googleapis.com/flutter_infra/releases/beta/linux/flutter_linux_v0.5.1-beta.tar.xz 10 | tar xf ~/development/flutter_linux_v0.5.1-beta.tar.xz 11 | 12 | export PATH=~/development/flutter/bin:$PATH 13 | 14 | 15 | cd $ROOT 16 | flutter packages get 17 | 18 | gem install coveralls-lcov 19 | -------------------------------------------------------------------------------- /dev/bots/travis_script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -ex 3 | 4 | export PATH=~/development/flutter/bin:$PATH 5 | export ROOT="$PWD" 6 | 7 | if [[ "$SHARD" == "dartfmt" ]]; then 8 | echo 'Formating code' 9 | cd $ROOT 10 | flutter format . || exit $? 11 | else 12 | # tests shard 13 | cd $ROOT 14 | 15 | flutter test --coverage test/* || exit $? 16 | 17 | fi 18 | -------------------------------------------------------------------------------- /disk_lru_cache.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | 4 | .packages 5 | .pub/ 6 | 7 | build/ 8 | 9 | .flutter-plugins 10 | -------------------------------------------------------------------------------- /example/.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /example/.idea/libraries/Dart_Packages.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | -------------------------------------------------------------------------------- /example/.idea/libraries/Dart_SDK.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/.idea/libraries/Flutter_Plugins.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /example/.idea/libraries/Flutter_for_Android.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /example/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /example/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /example/.idea/runConfigurations/main_dart.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /example/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 34 | 35 | 36 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 |