如何将文件放入 dotnet 的缓存中?
我一直在尝试通过JSON文件在.NET 6.0中实现本地化,并且一种方法(请注意,作为具有不同格式的JSON提供资源 iStringLocalizer 接口,当然,每次在查找中都不存在在 iDistributedCache中不存在,然后加载文件,相应地对其进行解析,然后将键值对插入分布式缓存。
但是,在分布式缓存中不存在的每个条目时,我将不得不加载文件(这是IO查找,我猜该应用程序相当昂贵,尤其是在经常有缓存失误的情况下)并获得值取决于我想从哪个文件中读取的文件。
在调用启动时,有没有办法加载文件,然后将其注入我制作的本地化器扩展名?
I have been trying to implement localization in .NET 6.0 via JSON files, and a way of doing that (please note that the resources are provided as JSONs with different formats eg one has nested objects and the other simply key-values) was to implement the IStringLocalizer interface and of course every time on lookup in something doesn't exist in the IDistributedCache then load the file, parse it accordingly and insert the key-value pair in the distributed cache.
However, upon every entry that doesn't exist in the distributed cache, I will have to load the file (which is IO lookup and I am guessing rather expensive for the app, especially if there is a cache miss frequently) and get the value depending from which file I want to read from.
Is there a way to load the file when startup is called and simply inject it to the localizer extension that I have made?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样调用在启动中管理本地化的服务
在那一刻应该构建 DI,以便您应该有权访问 IDistributedCache
You can invoke your service which manage localization in Startup like that
In that moment DI should be builded so you should have access to IDistributedCache