{"id":17859294,"url":"https://github.com/zeshuaro/data_cache_manager","last_synced_at":"2025-03-20T19:30:49.474Z","repository":{"id":37927291,"uuid":"345332145","full_name":"zeshuaro/data_cache_manager","owner":"zeshuaro","description":"A Flutter cache manager plugin for storing and managing Dart data types.","archived":false,"fork":false,"pushed_at":"2025-03-02T01:27:26.000Z","size":150,"stargazers_count":9,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T17:49:24.758Z","etag":null,"topics":["dart","firebase-database","firebase-realtime-database","flutter","flutter-plugin"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/data_cache_manager","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/zeshuaro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-07T11:45:32.000Z","updated_at":"2024-02-05T04:35:03.000Z","dependencies_parsed_at":"2024-10-28T06:53:43.396Z","dependency_job_id":"f19591f6-025e-4807-b254-c4c6a2f044e6","html_url":"https://github.com/zeshuaro/data_cache_manager","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeshuaro%2Fdata_cache_manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeshuaro%2Fdata_cache_manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeshuaro%2Fdata_cache_manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeshuaro%2Fdata_cache_manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeshuaro","download_url":"https://codeload.github.com/zeshuaro/data_cache_manager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244676454,"owners_count":20491828,"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":["dart","firebase-database","firebase-realtime-database","flutter","flutter-plugin"],"created_at":"2024-10-28T06:42:17.220Z","updated_at":"2025-03-20T19:30:48.835Z","avatar_url":"https://github.com/zeshuaro.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Cache Manager\n\nA Flutter cache manager plugin for storing and managing Dart data types.\n\n[![pub package](https://img.shields.io/pub/v/data_cache_manager.svg)](https://pub.dartlang.org/packages/data_cache_manager)\n[![docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://pub.dev/documentation/data_cache_manager/latest/)\n[![MIT License](https://img.shields.io/github/license/zeshuaro/data_cache_manager.svg)](https://github.com/zeshuaro/data_cache_manager/blob/main/data_cache_manager/LICENSE)\n[![data_cache_manager](https://github.com/zeshuaro/data_cache_manager/actions/workflows/data_cache_manager.yml/badge.svg)](https://github.com/zeshuaro/data_cache_manager/actions/workflows/data_cache_manager.yml)\n[![codecov](https://codecov.io/gh/zeshuaro/data_cache_manager/branch/main/graph/badge.svg?token=BA2LTD1XI1\u0026flag=data_cache_manager)](https://codecov.io/gh/zeshuaro/data_cache_manager)\n[![pedantic](https://img.shields.io/badge/style-pedantic-40c4ff.svg)](https://github.com/google/pedantic)\n\n## Getting Started\n\nAdd this to your project's `pubspec.yaml` file:\n\n```yml\ndependencies:\n  data_cache_manager: ^1.2.0\n```\n\n## Usage\n\n### Initialization\n\nThe easiest way is to use the provided `DefaultDataCacheManager.instance` to get a default instance of the cache manager:\n\n```dart\nimport 'package:data_cache_manager/data_cache_manager.dart';\n\nfinal DataCacheManager manager = DefaultDataCacheManager.instance;\n```\n\n### Adding data to cache\n\n`DataCacheManager` can store most of the data types in Dart, see [this](#what-data-types-are-supported) section for a list of supported data types.\n\nTo add data, simply use the following method:\n\n```dart\nfinal String key = 'key';       // The unique key for your data\nfinal String value = 'value';   // The data to cache\n\nawait manager.add(key, value);  // Add the data to local cache\n```\n\n**Storing different data with the same key**\n\nIn some cases, you might want to store different data with the same key. For example, when you are querying data from a database and paginating your data results. You can pass in the parameters you used to query the data as a `Map` when caching the data:\n\n```dart\n// The parameters you used to query the data in the first page\nfinal Map\u003cString, dynamic\u003e firstParams = {'page': 1};\n\n// The data on the first page\nfinal List\u003cint\u003e firstValue = [0, 1, 2, 3, 4];\n\n// The parameters you used to query the data in the second page\nfinal Map\u003cString, dynamic\u003e secondParams = {'page': 2};\n\n// The data on the second page\nfinal List\u003cint\u003e secondValue = [5, 6, 7, 8, 9];\n\n// Add the data to local cache\nawait manager.add(key, firstValue, queryParams: firstParams);\nawait manager.add(key, secondValue, queryParams: secondParams);\n```\n\n### Getting data from cache\n\nTo get data from the cache, simply use the same key that you used to store the data:\n\n```dart\nfinal cachedData = await manager.get(key);\n```\n\nIf you passed in `queryParams` when storing the data, you can also use the same `queryParams` to get the respective data from the cache:\n\n```dart\n// firstCache will be [0, 1, 2, 3, 4]\nfinal firstCache = await manager.get(key, queryParams: firstParams);\n\n// secondCache will be [5, 6, 7, 8, 9]\nfinal secondCache = await manager.get(key, queryParams: secondParams);\n```\n\nTo avoid getting outdated cached data, you can pass in a `DateTime` to tell the cache manager when the data has been updated. If the cached data is stored earlier than the given `DateTime`, it will return `null` and remove the outdated cache.\n\n```dart\nfinal DateTime updatedAt = DateTime.now();\n\n// cachedData will be null as it has been outdated\nfinal cachedData = await manager.get(key, updatedAt: updatedAt);\n```\n\n### Removing data from cache\n\nTo remove data from the cache, simply use the same key that you used to store the data:\n\n```dart\nawait manager.remove(key);\n```\n\nIf you passed in `queryParams` when storing the data, you can also use the same `queryParams` to remove the respective data from the cache:\n\n```dart\n// Removes [0, 1, 2, 3, 4]\nawait manager.remove(key, queryParams: firstParams);\n\n// Removes [5, 6, 7, 8, 9]\nawait manager.remove(key, queryParams: secondParams);\n```\n\nTo remove all the data with the same key, use the following method instead:\n\n```dart\nawait manager.removeByKey(key);\n```\n\n## Customization\n\nIf you want to customize and configure the cache manager, you will have to create your own class and store the cache manager instance. **It is important** to only create and use **one** cache manager instance with the same `dbKey` (which can be configured) throughout your application. See below for an example:\n\n```dart\nclass MyDataCacheManager {\n  static DataCacheManager instance = DataCacheManager(\n    config: Config(\n      dbKey: 'db_key',\n      useMemCache: true,\n      useNtpDateTime: false,\n      cleanupOnInit: false,\n      cleanupInterval: Duration(seconds: 30),\n      stalePeriod: Duration(days: 30),\n      maxCacheSize: 10000000,\n    ),\n  );\n}\n```\n\n## Other Implementations\n\n- [firebase_db_cache](https://github.com/zeshuaro/data_cache_manager/tree/main/firebase_db_cache) for caching data fetched from [firebase_database](https://pub.dev/packages/firebase_database)\n\n## Frequently Asked Questions\n\n### What data types are supported?\n\nThe supported data types are:\n- `int` \n- `double`\n- `String`\n- `bool`\n- `List`\n- `Map`\n\nNote that:\n- `Iterable` is not supported, use `toList()` to convert any iterables\n- `List` and `Map` has to be storing the types specified above\n\n### When do the cached data get removed?\n\nYou can use the `stalePeriod` and `maxCacheSize` parameters in `Config` to control when the data get removed.\n\n- `stalePeriod` can be used to define the minimum duration for a cached data to become stale. When the data becomes stale, it will be removed.\n- `maxCacheSize` is the maximum cache size in bytes. When the cache size gets over this limit, it will start removing data. The data that has been stale for the longest period of time and has the least amount of usage will be removed first and so on.\n\n### How to schedule the cached data cleanup?\n\nYou can use the `cleanupOnInit` and `cleanupInterval` parameters in `Config` to setup when to run and clean the cached data.\n\n- Setting `cleanupOnInit` to `true` will only run the cleanup once when `DataCacheManager` is initialized. This will also override `cleanupInterval` to ensure that the cleanup is only run once.\n- You can also define `cleanupInterval` to set how often the cache manager should run and clean the data.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeshuaro%2Fdata_cache_manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeshuaro%2Fdata_cache_manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeshuaro%2Fdata_cache_manager/lists"}