{"id":13536772,"url":"https://github.com/andrew0000/universal-cache","last_synced_at":"2025-04-02T03:31:07.389Z","repository":{"id":78654406,"uuid":"592076216","full_name":"Andrew0000/Universal-Cache","owner":"Andrew0000","description":"Kotlin caching and request sharing via Flow. Main idea: don't load data more times than it's needed. Compiled for Android and iOS.","archived":false,"fork":false,"pushed_at":"2025-03-13T10:19:23.000Z","size":238,"stargazers_count":75,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T11:26:38.670Z","etag":null,"topics":["android","cache","ios","kotlin","kotlin-coroutines","kotlin-flow","kotlin-multiplatform"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Andrew0000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2023-01-22T20:55:49.000Z","updated_at":"2025-03-13T10:19:28.000Z","dependencies_parsed_at":"2025-03-13T18:32:11.864Z","dependency_job_id":null,"html_url":"https://github.com/Andrew0000/Universal-Cache","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Andrew0000%2FUniversal-Cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Andrew0000%2FUniversal-Cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Andrew0000%2FUniversal-Cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Andrew0000%2FUniversal-Cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Andrew0000","download_url":"https://codeload.github.com/Andrew0000/Universal-Cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246751040,"owners_count":20827822,"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":["android","cache","ios","kotlin","kotlin-coroutines","kotlin-flow","kotlin-multiplatform"],"created_at":"2024-08-01T09:00:49.248Z","updated_at":"2025-04-02T03:31:07.383Z","avatar_url":"https://github.com/Andrew0000.png","language":"Kotlin","funding_links":[],"categories":["Libraries"],"sub_categories":["Storage","📦 Storage"],"readme":"# Universal Cache (Kotlin / Coroutines / Flow)\n\n### Goal: Make cache and a request sharing with Kotlin in a simple way.\n\nSome functions:  \n✔️ Share ongoing request(s) by parameters as key.  \n✔️ Cache results of every request (in-memory / custom cache).  \n✔️ Provide a convenient function to get cached or request new data based on passed arguments.  \n\nThe real-life reason and inspiration is to reduce network workload in big applications \nwith a lot of independent components that may use the same API-endpoints at the nearly same time.\n\n# Usage:\n\nBasic case:  \n\n```kotlin\nval cachedSource = CachedSource\u003cString, Int\u003e(\n    source = { params -\u003e api.getSomething(params) }\n)\n\nscope.launch {\n    cachedSource.get(\"some request parameter\", FromCache.CACHED_THEN_LOAD, maxAge = 5_000)\n        .catch { \n            // Handle error if needed\n        }\n        .collect {\n            // Use received value(s)\n        }\n}\n```\n\n### CachedSource  \nWrapper for your data source (usually API call or DB-request). Provides Flow of data or errors in several ways.  \nConstruct it with your data source and optional parameters:  \n```\nval cachedSource = CachedSource\u003cP, T\u003e(\n    source = { api.getSomething() },\n    cache = ...,\n    timeProvider = ...,\n    dispatcher = ...,\n)\n```\nUse `.get()` or `.getRaw()` function with a variety of options to get a Flow of cached / loaded data:\n```\ncachedSource\n    .get(\n        params = ...,\n        fromCache = ...,\n        shareOngoingRequest = ...,\n        maxAge = ...,\n        additionalKey = ...,\n    )\n    .collect {\n        ...\n    }\n```\nUse `.updates` and `.errors` (SharedFlow) to observe all updates and errors without `.get()`:\n```\ncachedSource.updates\n    .collect { (params, result) -\u003e\n        ...\n    }\n```\n\n### FromCache\nEnum with request options:\n```\nFromCache.NEVER\nFromCache.IF_FAILED\nFromCache.IF_HAVE\nFromCache.ONLY\nFromCache.CACHED_THEN_LOAD\n```\n\n### Requester  \nWrapper that can share ongoing request(s) if there is any in progress with the given params.  \nIt's used in **CachedSource** but you can use it independently.  \n  \nMain differences from **shareIn** + **WhileSubscribed**:  \n* **Requester** focuses on the emission of a single value.\n* Rethrows possible exception to downstreams (**shareIn** swallows exceptions and trows to the specified scope).\n* Can share multiple streams with distinct keys at the same time.\n```\nval requester = Requester(source)\n...\nrequester.requestShared(\"some request params\")\n    .collect {\n        ...\n    }\n```\n\n### Cache  \nInterface for a how you store your cache. There is default in-memory implementation **MemoryCache**.  \nMain functions:\n```\nget(params: P, additionalKey: Any?): CachedData\u003cT\u003e?\n\nput(value: T, params: P, additionalKey: Any?, time: Long)\n```\n\n# Setup:  \n\n### Kotlin Multiplatform. Versions after 1.1.*  \n\n[![Maven Central](https://img.shields.io/maven-central/v/io.github.andrew0000/universal-cache)](https://mvnrepository.com/artifact/io.github.andrew0000/universal-cache)  \n\nAdd `implementation 'io.github.andrew0000:universal-cache:$latest_version'` to the module-level `build.gradle` \n\n### Old. Android only. Versions prior to 1.1.*  \n\n[![](https://jitpack.io/v/Andrew0000/Universal-Cache.svg)](https://jitpack.io/#Andrew0000/Universal-Cache)\n\n1. Add `maven { url 'https://jitpack.io' }` to the `allprojects` or `dependencyResolutionManagement` section in top-level `build.gradle` or `settings.gradle`.  \nFor example (`settings.gradle`):\n```\ndependencyResolutionManagement {\n    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n    repositories {\n        google()\n        mavenCentral()\n        maven { url \"https://jitpack.io\" }\n    }\n}\n```\n2. Add `implementation 'com.github.Andrew0000:Universal-Cache:$latest_version'` to the module-level `build.gradle`  \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrew0000%2Funiversal-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrew0000%2Funiversal-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrew0000%2Funiversal-cache/lists"}