{"id":21947930,"url":"https://github.com/tinystuff/tinycache","last_synced_at":"2025-04-23T00:03:23.525Z","repository":{"id":27930004,"uuid":"115608194","full_name":"TinyStuff/TinyCache","owner":"TinyStuff","description":"Small helper for offline and caching of long-running processes","archived":false,"fork":false,"pushed_at":"2022-12-07T18:27:58.000Z","size":358,"stargazers_count":39,"open_issues_count":10,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-24T19:52:08.681Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","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/TinyStuff.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}},"created_at":"2017-12-28T09:35:36.000Z","updated_at":"2024-03-20T22:47:00.000Z","dependencies_parsed_at":"2023-01-14T07:45:59.929Z","dependency_job_id":null,"html_url":"https://github.com/TinyStuff/TinyCache","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/TinyStuff%2FTinyCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TinyStuff%2FTinyCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TinyStuff%2FTinyCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TinyStuff%2FTinyCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TinyStuff","download_url":"https://codeload.github.com/TinyStuff/TinyCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227048645,"owners_count":17723256,"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-29T05:10:42.315Z","updated_at":"2024-11-29T05:10:42.931Z","avatar_url":"https://github.com/TinyStuff.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TinyCache\nSmall helper for offline and caching of long-running processes\n\n## Build status\n![buildstatus](https://io2gamelabs.visualstudio.com/_apis/public/build/definitions/be16d002-5786-41a1-bf3b-3e13d5e80aa0/14/badge)\n\n## Breaking changes in version 2.0\nVersion 2.0.x will introduce breaking changes to how to use TinyCache. Pre 2.0 we could only have one instance of TinyCache. 2.0 introduces the option to have multiple instances of TinyCache. To make that possible we have introduced TinyCacheHandler.\n\n## TinyCacheHandler\nIf we only want one instance of TinyCache, we can use the **Default** method of **TinyCacheHandler**. First time you access the Default property a new instance of TinyCache will be created if there are not instances created. This instance will get **default** as the key.\n\n### Create an additional instance of TinyCache\nTo add a new instance of TinyCacheHandler we can use either the create method or the add method. \n\n\n**Using the Create method:**\n```csharp\nvar newCache = TinyCacheHandler.Create(\"myNewCache\");\n```\n\n**Using the Add method:**\n```csharp\nvar newCache = new TinyCache();\nTinyCacheHandler.Add(\"myNewCache\", newCache);\n```\n### Set default cache\nIf we have multiple cache instances, we maybe not want the first one to be default, then we can change that by passing the cahce key to the SetDefault method of TinyCacheHandler.\n\n```csharp\nTinyCacheHandler.SetDefault(\"myNewCache\");\n```\n\n### Get a specific instance of TinyCache\nIf we want to get an instance of TinyCache that not are the default instance, we can use the key for the instance as in the code below.\n```csharp\nvar cache = TinyCacheHandler.Get(\"myNewCache\");\n\nvar result = cache.RunAsync\u003cList\u003cData\u003e\u003e(\"cachekey\", () =\u003e { return api.GetData(\"customdata\"); });\n```\n\n## Example\n\n### Use file storage\nInstall NuGet package TinyCache.FileStorage.\n\n```csharp\n// Create a cache storage, in memory cache will be the default.\nvar store = new FileStorage();\n\nvar cacheFolder = string.Empty;\n            \n#if __IOS__ || __MACOS__\n            cacheFolder = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User)[0];\n#elif __ANDROID__\n            cacheFolder = Application.Context.CacheDir.AbsolutePath;\n#elif __UWP__\n            cacheFolder = Windows.Storage.ApplicationData.Current.LocalFolder.Path;\n#else\n            cacheFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);\n#endif\n\nstore.Initialize(cacheFolder);\n\n// Set cache storage\nTinyCache.TinyCache.SetCacheStore(store);\n\n// Fetch data with default policy\nvar result = await TinyCacheHandler.Default.RunAsync\u003cList\u003cData\u003e\u003e(\"cachekey\", () =\u003e { return api.GetData(\"customdata\"); });\n```\n\n### Use property storage in Xamarin.Forms\nInstall NuGet package TinyCache.Forms.\n\n```csharp\n// Create a cache storage, in memory cache will be the default.\nvar store = new XamarinPropertyStorage();\n\n// Set cache storage\nTinyCacheHandler.Default.SetCacheStore(store);\n\n// Fetch data with default policy\nvar result = await TinyCacheHandler.Default..RunAsync\u003cList\u003cData\u003e\u003e(\"cachekey\", () =\u003e { return api.GetData(\"customdata\"); });\n\n```\n## Caching DelegationHandler\n```csharp\nAuoRestApi api = new AuoRestApi(apiEndPoint, new NoClientCredentials(), new TinyCache.TinyCacheDelegationHandler());\n```\n## Some extra examples\nNot needed, but nice to have\n```csharp\n\n// Handle errors\nTinyCache.TinyCache.OnError += (sender, e) =\u003e\n{\n    ShowError(e);\n};\n\n// Set a base policy that will be used when no policy is specified\nTinyCacheHandler.Default..SetBasePolicy(\n    new TinyCachePolicy()\n        .SetMode(TinyCacheModeEnum.CacheFirst) // fetch from cache first\n        .SetFetchTimeout(TimeSpan.FromSeconds(5)) // 5 second excecution limit\n        .SetExpirationTime(TimeSpan.FromMinutes(10)) // 10 minute expiration before next fetch\n        .SetUpdateCacheTimeout(50) // Wait 50ms before trying to update cache in background\n        .UpdateHandler = async (key, newdata) =\u003e { await DoStuff(key, newdata); }); // Handle background updates \n\n// Handle background changes\nTinyCacheHandler.Default.OnUpdate += async (object sender, CacheUpdatedEvt e) =\u003e {\n    var cacheKey = e.Key;\n    var dataObject = e.Value;\n    async HandleObjectChange(cacheKey,dataObject as MyDataType);\n};\n\n```\n## Some extra for preloading cache\nNot needed, but nice to have\n```csharp\n\n// Get all cached data from storage (to be saved in static file in project and then loaded)\nvar preloadString = store.GetAllAsLoadableString();\n\n// Preload cache if needed from stored string:\nstore.LoadFromString(CacheResources.PreloadData.JsonData);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinystuff%2Ftinycache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinystuff%2Ftinycache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinystuff%2Ftinycache/lists"}