C# 实现应用程序范围的缓存 [英] C# implementing a application wide cache

查看:38
本文介绍了C# 实现应用程序范围的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们要翻译"标签.我不想为同一个标签多次访问数据库.因此,当我已经获取了术语 X 时,我想从缓存中获取该术语.

In our application we want to 'translate' labels. I don't want to hit the database multiple times for the same label. So when I already have fetched the term X I want to get this term from the cache.

现在我正在考虑如何实现这一点.我可以想到这个选项:

Now I am thinking on how to implement this. I could think of this options:

  1. 创建一个单例.所以基本上你正在创建一个公共变量
  2. 创建一个带有静态列表的类,其中包含缓存的翻译.

你应该怎么做?

我使用的是 C# winforms.

I am using C# winforms.

我指的不是语言翻译,而是术语翻译.我们的客户对同一件事有自己的术语.所以这是我们应用程序中的一个设置.假设您有术语 X,他们可以说,我想称其为 Z.因此,在应用程序中使用 X 的任何地方,都必须显示 Z.

I don't mean lanquage translation, but term translation. Our customers have their own term for the same thing. So this is a setting in our application. Say you have term X, they can say, I'd like to call the Z. So everywhere in the application where X is used, Z must be displayed.

每个表单都有一些带有(相同)术语的标签.所以数据本身很小(只有一个词),但有可能进入数据库20次才能得到一种形式的词条.

Every form has a few labels with (the same) terms. So the data itself is small (only one word), but it is possible that it goes into the database 20 times to get the terms for one form.

推荐答案

ASP.NET HttpCache 也可以在 ASP.NET 之外使用.您可以将其用作缓存的支持机制,并通过 Singleton 访问它.

The ASP.NET HttpCache is also available outside ASP.NET. You could use this as a backing mechanism for your cache and access it trough a Singleton.

如果它适合您的场景,另一个选项可能是 T4 模板,它可以解析您的数据库并在编译时生成一个类.这比在运行时查找数据库要快得多.仅当数据库中的某些内容发生更改时,您才必须重新运行 T4 并部署新程序集.

Another option if it fits your scenario could be a T4 template that would parse your database and generate a class at compile time. This is way faster that doing the database look up at run time. Only if something in your database changes you would have to rerun your T4 and deploy the new assembly.

这篇关于C# 实现应用程序范围的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆