{"id":17051840,"url":"https://github.com/glebbatykov/mcache","last_synced_at":"2026-01-11T04:49:20.628Z","repository":{"id":63036227,"uuid":"558408185","full_name":"GlebBatykov/mcache","owner":"GlebBatykov","description":"Tools for caching values in memory. Supports setting the expiration of values.","archived":false,"fork":false,"pushed_at":"2022-11-11T16:42:02.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T01:46:54.415Z","etag":null,"topics":["cache","caching","dart","memory","memory-cache"],"latest_commit_sha":null,"homepage":"","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/GlebBatykov.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":"2022-10-27T13:45:05.000Z","updated_at":"2022-11-11T11:30:56.000Z","dependencies_parsed_at":"2023-01-22T15:15:55.739Z","dependency_job_id":null,"html_url":"https://github.com/GlebBatykov/mcache","commit_stats":null,"previous_names":["glebbatykov/cache"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlebBatykov%2Fmcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlebBatykov%2Fmcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlebBatykov%2Fmcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GlebBatykov%2Fmcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GlebBatykov","download_url":"https://codeload.github.com/GlebBatykov/mcache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245056912,"owners_count":20553856,"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":["cache","caching","dart","memory","memory-cache"],"created_at":"2024-10-14T10:07:39.871Z","updated_at":"2026-01-11T04:49:20.622Z","avatar_url":"https://github.com/GlebBatykov.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n[![pub package](https://img.shields.io/pub/v/mcache.svg?label=mcache\u0026color=blue)](https://pub.dev/packages/mcache)\n\n**Languages:**\n\n[![English](https://img.shields.io/badge/Language-English-blue?style=?style=flat-square)](README.md)\n[![Russian](https://img.shields.io/badge/Language-Russian-blue?style=?style=flat-square)](README.ru.md)\n\n\u003c/div\u003e\n\n- [About Cache](#about-cache)\n- [Set](#set)\n- [Get](#get)\n- [Update](#update)\n- [Delete](#delete)\n- [Has](#has)\n- [Expiration](#expiration)\n  - [Change expiration](#change-expiration)\n  - [Delete callback](#delete-callback)\n- [Clear](#clear)\n\n# About Cache\n\n`Cache` provides:\n\n- caching of values in memory;\n- automatic deletion of values after expiration.\n\n# Set\n\nTo cache values, use the `set` method.\n\nExample of using the `set` method to set values:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex');\n}\n```\n\n# Get\n\nTo get the values, use the `get` method or the `[]` operator.\n\nExample of getting values by key:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex');\n\n  print(cache.get('name'));\n\n  print(cache['cache']);\n}\n```\n\nExpected output:\n\n```dart\nAlex\nAlex\n```\n\n# Update\n\nYou can update an existing value and update its lifetime using the `update` method.\n\nExample of updating a value:\n\n```dart\nvoid main(List\u003cString\u003e args) {\n  var cache = Cache(checkPeriod: const Duration(milliseconds: 250));\n\n  cache.set('name', 'Alex',\n      expirationSetting:\n          ExpirationSetting(expiration: const Duration(seconds: 1)));\n\n  Future.delayed(const Duration(milliseconds: 500), () {\n    cache.update('name', 'Tom');\n  });\n\n  Future.delayed(const Duration(milliseconds: 1300), () {\n    print(cache['name']);\n  });\n\n  Future.delayed(const Duration(milliseconds: 1600), () {\n    print(cache['name']);\n\n    cache.dispose();\n  });\n}\n```\n\nExpected output:\n\n```dart\nTom\nnull\n```\n\n# Delete\n\nYou can delete a value using the `delete` method.\n\nExample of deleting a value:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex');\n\n  print(cache['name']);\n\n  cache.delete('name');\n\n  print(cache['name']);\n}\n```\n\nExpected output:\n\n```dart\nAlex\nnull\n```\n\n# Has\n\nYou can check the presence of a key value using the `has` method.\n\nExample of checking for the presence of a value:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex');\n\n  print(cache.has('name'));\n\n  print(cache.has('key'));\n}\n```\n\nExpected output:\n\n```dart\ntrue\nfalse\n```\n\n# Expiration\n\nIn `Cache` there is an automatic clearing of values after a specified time.\n\nYou can set the lifetime of the value in the cache using the `expirationSetting` parameter of the `set` method.\n\nExample of setting the lifetime of a value:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex',\n      expirationSetting:\n          ExpirationSetting(expiration: const Duration(minutes: 15)));\n}\n```\n\nTo clear the `Cache`, there is an interval for checking the values to see if their lifetime has expired. By default, it is 600 seconds.\n\nYou can independently set the time of the interval for checking values using the `checkPeriod` parameter when creating an instance of the `Cache` class.\n\nExample of setting the interval for checking values:\n\n```dart\nvoid main() {\n    var cache = Cache(checkPeriod: const Duration(seconds: 5));\n}\n```\n\nIf you do not plan to automatically clear the values after a while, you can disable this by using the `deleteOnExpire` parameter. By default, it is true.\n\n## Change expiration\n\nAfter writing the value, you can change its lifetime using the `changeExpiration` method.\n\nExample of changing the lifetime of a value:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex',\n      expirationSetting:\n          ExpirationSetting(expiration: const Duration(minutes: 5)));\n\n  cache.changeExpiration(\n      'name', ExpirationSetting(expiration: const Duration(minutes: 1)));\n}\n```\n\n## Delete callback\n\nWhen setting the lifetime of a value, you can also assign a handler that will trigger when the value is deleted after its expiration.\n\nIf the handler returns true, the value will be deleted. If the handler returns false, the lifetime of the value will be extended by the time set for it.\n\nExample of installing a handler:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex',\n      expirationSetting: ExpirationSetting(\n          expiration: const Duration(minutes: 5),\n          onDelete: (value) {\n            print(value);\n\n            return true;\n          }));\n}\n```\n\n# Clear\n\nYou can clear all the values in the cache using the `clear` method.\n\nExample of clearing the cache value:\n\n```dart\nvoid main() {\n  var cache = Cache();\n\n  cache.set('name', 'Alex');\n\n  print(cache['name']);\n\n  cache.clear();\n\n  print(cache['name']);\n}\n```\n\nExpected output:\n\n```dart\nAlex\nnull\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglebbatykov%2Fmcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglebbatykov%2Fmcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglebbatykov%2Fmcache/lists"}