{"id":18868407,"url":"https://github.com/best-flutter/disk_lru_cache","last_synced_at":"2025-10-26T12:05:50.893Z","repository":{"id":56828099,"uuid":"144393555","full_name":"best-flutter/disk_lru_cache","owner":"best-flutter","description":"Disk lru cache for flutter.","archived":false,"fork":false,"pushed_at":"2020-01-09T13:38:57.000Z","size":166,"stargazers_count":31,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T03:51:13.169Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/best-flutter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-11T14:50:07.000Z","updated_at":"2024-01-07T07:42:01.000Z","dependencies_parsed_at":"2022-08-29T02:40:42.938Z","dependency_job_id":null,"html_url":"https://github.com/best-flutter/disk_lru_cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fdisk_lru_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fdisk_lru_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fdisk_lru_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fdisk_lru_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/best-flutter","download_url":"https://codeload.github.com/best-flutter/disk_lru_cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248897225,"owners_count":21179560,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T05:13:34.310Z","updated_at":"2025-10-26T12:05:45.843Z","avatar_url":"https://github.com/best-flutter.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://travis-ci.org/best-flutter/disk_lru_cache\"\u003e\n        \u003cimg src=\"https://travis-ci.org/best-flutter/disk_lru_cache.svg?branch=master\" alt=\"Build Status\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://coveralls.io/github/best-flutter/disk_lru_cache?branch=master\"\u003e\n        \u003cimg src=\"https://coveralls.io/repos/github/best-flutter/disk_lru_cache/badge.svg?branch=master\" alt=\"Coverage Status\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/best-flutter/disk_lru_cache/pulls\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/PRs-Welcome-brightgreen.svg\" alt=\"PRs Welcome\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pub.dartlang.org/packages/disk_lru_cache\"\u003e\n        \u003cimg src=\"https://img.shields.io/pub/v/disk_lru_cache.svg\" alt=\"pub package\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\n\n# disk_lru_cache\nDisk lru cache for flutter. [wiki](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU))\n\nA cache that uses a bounded amount of space on a filesystem. \nEach cache entry has a string key and a fixed number of files, witch is accessible as stream.\n\n## Use cases\n\n\n#### Working with memery\n\nWe 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\n\n\n\n```\nfinal LruMap\u003cString, int\u003e map = new LruMap();\n\nexpect(map.values.toList().length, 0);\n\nmap['a'] = 1;\nmap['b'] = 2;\nmap['c'] = 3;\n\n/// use the key 'a'\nvar f = map['a'];\n\n/// We use the key 'a', so at this moment it is the last element.\nalues = map.values;\nexpect(values.toList()[0], 2);\nexpect(values.toList()[1], 3);\nexpect(values.toList()[2], 1);\n\n```\n\n\n#### Working with file system\n\nThe basic usage is like this:\n\n\nWith string:\n\n```\nint maxSize =\n      10 * 1024 * 1024; // 10M\n\n// Make sure it's writable\nDirectory cacheDirectory =\n            new Directory(\"${Directory.systemTemp.path}/cache\");\n\n DiskLruCache cache = new DiskLruCache(\n        maxSize: maxSize, directory: cacheDirectory, filesCount: 1);\n\n    // write stream\n    CacheEditor editor = await cache.edit('filekey');\n    if(editor!=null){\n      IOSink sink = await editor.newSink(0);\n      sink.write('your value');\n      await sink.close();\n      await editor.commit();\n    }\n\n    // read stream\n    CacheSnapshot snapshot =  await cache.get('filekey');\n    String str = await snapshot.getString(0);\n    print(str);\n\n```\n\n\nWith bytes\n\n```\n// write bytes\n  CacheEditor editor = await cache.edit('imagekey');\n  if(editor!=null){\n    HttpClient client = new HttpClient();\n    HttpClientRequest request = await client.openUrl(\"GET\", Uri.parse(\"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image\u0026quality=100\u0026size=b4000_4000\u0026sec=1534075481\u0026di=1a90bd266d62bc5edfe1ce84ac38330e\u0026src=http://photocdn.sohu.com/20130517/Img376200804.jpg\"));\n    HttpClientResponse response = await request.close();\n    Stream\u003cList\u003cint\u003e\u003e stream = await editor.copyStream(0, response);\n    // The bytes has been written to disk at this point.\n    await new ByteStream(stream).toBytes();\n    await editor.commit();\n\n    // read stream\n    CacheSnapshot snapshot =  await cache.get('imagekey');\n    Uint8List bytes = await snapshot.getBytes(0);\n    print(bytes);\n  }\n\n```\n\n\n## Manage the cache\n\n\n#### Get the bytes of the cache in file system\n\n```\nDiskLruCache cache = ...;\nprint(cache.size)\n```\n\n#### Clean the cache\n\n```\nDiskLruCache cache = ...;\ncache.clean();\n\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbest-flutter%2Fdisk_lru_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbest-flutter%2Fdisk_lru_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbest-flutter%2Fdisk_lru_cache/lists"}