百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

LazyCache

> 数据库
开源

一个易于使用的线程安全的内存缓存服务,具有简单易用的 API,适用于 C#

1.8K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个易于使用的线程安全的内存缓存服务,具有简单易用的 API,适用于 C#

Lazy Cache

2025 update - HybridCache is available and part of the framework now and solves most of the same problems as LazyCache. Consider using it in new projects and migrating old projects to use it as Microsft will likely provide better long term support

Lazy cache is a simple in-memory caching service. It has a developer friendly generics based API, and provides a thread safe cache implementation that guarantees to only execute your cachable delegates once (it's lazy!). Under the hood it leverages Microsoft.Extensions.Caching and Lazy to provide performance and reliability in heavy load scenarios.

Download

LazyCache is available using nuget. To install LazyCache, run the following command in the Package Manager Console

PM> Install-Package LazyCache

Quick start

See the quick start wiki

Sample code

…

As you can see the magic happens in the GetOrAdd() method which gives the consumer an atomic and tidy way to add caching to your code. It leverages a factory delegate Func and generics to make it easy to add cached method calls to your app.

It means you avoid the usual "Check the cache - execute the factory function - add results to the cache" pattern, saves you writing the double locking cache pattern and means you can be a lazy developer!

What should I use it for?

LazyCache suits the caching of database calls, complex object graph building routines and web service calls that should be cached for performance. Allows items to be cached for long or short periods, but defaults to 20 mins.

.Net framework and dotnet core support?

The latest version targets netstandard 2.0. See .net standard implementation support

For dotnet core 2, .net framwork net461 or above, netstandard 2+, use LazyCache 2 or above.

For .net framework without netstandard 2 support such as net45 net451 net46 use LazyCache 0.7 - 1.x

For .net framework 4.0 use LazyCache 0.6

Features

  • Simple API with familiar sliding or absolute expiration
  • Guaranteed single evaluation of your factory delegate whose results you want to cache
  • Strongly typed generics based API. No need to cast your cached objects every time you retrieve them
  • Stops you inadvertently caching an exception by removing Lazys that evaluate to an exception
  • Thread safe, concurrency ready
  • Async compatible - lazy single evaluation of async delegates using GetOrAddAsync()
  • Interface based API and built in MockCache to support test driven development and dependency injection
  • Leverages a provider model on top of IMemoryCache under the hood and can be extended with your own implementation
  • Good test coverage

Documentation

  • The wiki
  • Adding caching to a .net application and make it faster

Sample Application

See CacheDatabaseQueriesApiSample for an example of how to use LazyCache to cache the results of an Entity framework query in a web api controller. Watch how the cache saves trips to the database and results are returned to the client far quicker from the in-memory cache

Contributing

If you have an idea or want to fix an issue please open an issue on Github to discuss it and it will be considered.

If you have code to share you should submit a pull request: fork the repo, then create a branch on that repo with your changes, when you are happy create a pull Request from your branch into LazyCache master for review. See https://help.github.com/en/articles/creating-a-pull-request-from-a-fork.

LazyCache is narrow in focus and well established so unlikely to accept massive changes out of nowhere but come talk about on GitHub and we can all collaborate on something that works for everyone. It is also quite extensible so you may be able to extend it in your project or add a companion library if necessary.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#c-sharpcacheinmemorylazy

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库