{"id":20215633,"url":"https://github.com/c12i/simpl_cache","last_synced_at":"2026-02-04T17:32:52.322Z","repository":{"id":146761570,"uuid":"617842431","full_name":"c12i/simpl_cache","owner":"c12i","description":"in memory ttl-caching tools","archived":false,"fork":false,"pushed_at":"2024-04-01T15:07:53.000Z","size":92,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-13T10:36:30.285Z","etag":null,"topics":["proc-macro-attributes","ttl-cache"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/simpl_cache","language":"Rust","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/c12i.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":"2023-03-23T08:20:07.000Z","updated_at":"2024-09-23T20:09:41.000Z","dependencies_parsed_at":"2024-11-14T12:35:45.843Z","dependency_job_id":null,"html_url":"https://github.com/c12i/simpl_cache","commit_stats":null,"previous_names":["collinsmuriuki/simple_cache","c12i/simpl_cache"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/c12i/simpl_cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fsimpl_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fsimpl_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fsimpl_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fsimpl_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c12i","download_url":"https://codeload.github.com/c12i/simpl_cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fsimpl_cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29091871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-04T03:31:03.593Z","status":"ssl_error","status_checked_at":"2026-02-04T03:29:50.742Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["proc-macro-attributes","ttl-cache"],"created_at":"2024-11-14T06:23:43.033Z","updated_at":"2026-02-04T17:32:52.299Z","avatar_url":"https://github.com/c12i.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simpl_cache\nSimple rust caching tools\n\n## Usage\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\nsimpl_cache = \"2.4.1-beta\"\n```\n\n## `ttl_cache` macro\n\nThis proc macro is designed to cache function calls with a time-to-live (TTL) duration. \nIt is useful when working with functions that perform expensive computations and have\noutputs that don't change frequently.\n\nThe macro generates a static variable for the cache that is shared across all calls to \nthe function with the same name and input arguments.\n\nIf a cached value is available, it is returned instead of recomputing the result. \nIf the cached value has expired or the function is called with different arguments,\nthe function will be recomputed and the cache will be updated with the new value.\n\n\n```rust\nuse simpl_cache::ttl_cache;\n\n#[ttl_cache(duration_s = 30)]\nfn fibonacci(n: u32) -\u003e u32 {\n    if n \u003c 2 {\n        return n;\n    }\n\n    fibonacci(n - 1) + fibonacci(n - 2)\n}\n\nfn main() {\n    println!(\"first: {}\", fibonacci(10)); // cache miss: return value is cached\n    println!(\"second: {}\", fibonacci(10)); // cached hit: cached value is returned\n    println!(\"last: {}\", fibonacci(20)); // cache miss: args changed, new result is cached\n}\n```\n\nYou can also cache the `Ok(T)` variant of a function returning a `Result\u003cT, E\u003e`:\n\n```rust\nuse simpl_cache::ttl_cache;\n\n// only_ok option ensures that only .is_ok values from the returning Result are cached\n#[ttl_cache(duration_s = 30, only_ok = true)] \nfn some_fallible_function(n: u32) -\u003e Result\u003cu32, String\u003e {\n    if n == 0 {\n        return Err(String::from(\"zeros are not allowed\"))\n    }\n    Ok(n)\n}\n\nfn main() {\n     // zero is not cached since function returns an Err since n == 0\n    println!(\"last: {:?}\", some_fallible_function(0));\n    // cache miss: 10 is cached since the result is_ok\n    println!(\"last: {:?}\", some_fallible_function(10));\n    // cache hit: 10 is retrieved from the cache\n    println!(\"last: {:?}\", some_fallible_function(10));\n\n}\n```\n\nSimilarly you can also chose to only cache `Some(T)` variants from a function returning an `Option\u003cT\u003e`\n\n```rust\nuse simpl_cache::ttl_cache;\n\n// only_some option ensures that only .is_some values from the returning Option are cached\n#[ttl_cache(duration_s = 30, only_some = true)] \nfn some_optional_function(n: u32) -\u003e Option\u003cu32\u003e {\n    if n == 0 {\n        return None;\n    }\n    Some(n)\n}\n\nfn main() {\n     // zero is not cached since function returns None since n == 0\n    println!(\"last: {:?}\", some_optional_function(0));\n    // cache miss: 10 is cached since the result is_some\n    println!(\"last: {:?}\", some_optional_function(10));\n    // cache hit: 10 is retrieved from the cache\n    println!(\"last: {:?}\", some_optional_function(10));\n\n}\n```\n\n## Notes\nFirstly, this is still a work in progress, so I would not advise using this in a production setting.\n\n⚠️ The macro is not stable for use with struct and enum methods, specifically those with `self` as an arg. ⚠️\n\nNote that `only_some` and `only_ok` can only be used when the annotated function returns an\n`Option\u003cT\u003e` or `Result\u003cT, E\u003e` respectively. You can also not set both `only_some` and `only_ok`\n\nThe macro will also not allow you to apply it to a function that does not return or explicitly \nreturns a unit type `()`. For example, the following will not compile:\n\n```rust,ignore\nuse simpl_cache::ttl_cache;\n\n#[ttl_cache(duration_s = 60)]\nfn print_hello_world() {\n    println!(\"Hello, world!\");\n}\n```\n\nFinally, the type returned by the annotated function must implement `Clone`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc12i%2Fsimpl_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc12i%2Fsimpl_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc12i%2Fsimpl_cache/lists"}