Martijn
Martijn

Reputation: 24789

C# implementing a application wide cache

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. Create a Singleton. So basically you are creating a public variable
  2. Create a class with a static list on it which contains the cached translations.

What should you do?

I am using C# winforms.

Edit: 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.

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.

Upvotes: 0

Views: 1011

Answers (2)

Akshita
Akshita

Reputation: 867

why don't you try Application Settings. while i don't recommend it for large amount of data bcoz it tend to put a load on application and is not intented for storing large data but you may use it if your data volume is small .

Go to Project>Properties>Settings Tab and add a setting of type User of Type Dataset (Use Browse Option for more types) or something more closer to your needs. In the Runtime assign or retrieve using

<datatype> obj=Properties.Settings.Default.<yoursettingname>;

Adding Settings

enter image description here

Upvotes: 0

Wouter de Kort
Wouter de Kort

Reputation: 39898

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.

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.

Upvotes: 2

Related Questions