C# implementing a application wide cache - c#

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:
Create a Singleton. So basically you are creating a public variable
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.

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.

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>;

Related

Searching a datatable versus searching through a List<>

First, my game is being made in Unity3D, utilizing a sqlite database.
Second, my database is used to store thousands of random star systems and their accompanying data (planets, stars, location etc etc)
What I want players to be able to do is to type in a systems "address" (for instance if there are 5 star systems inthe DB;
Sys001
Sys0012
Sys0013
Sys004
Sys050
and the user searches for 001, then the search results will return;
Sys001, Sys0012, Sys0013
ignoring Sys004 and Sys050.
This will happen instantly/near instantly (extremely quickly) so if the user then adds another character (IE searches for 0013) then the results will update in real-time instantly.
My question is....to achieve this, which would be better to use? Should I stick to just using my Database (IE using SQL functions to get this data) or would it be faster/better/easier/more ideal to import all database data into Lists, and then search the list (ie List[i].nameVariable.Contains("001") etc)
If it's being accessed a lot and is used often, then yes, you can load it into memory in a collection and use that. If not, create an index on the address and search on the column. Again, if the DB is accessed often then open a connection at game start, reuse that connection and close at the end. If not, open it and close after use in those rate instances you need to access it - this just helps free up resources a bit.

Passing data between instances of ASP.NET programs. Have I got the right idea?

I am a little confused as to how ASP.NET works. To my understanding, each time a webpage is created, it is an instance of the ASP.NET program. First of all, is this correct? For my website I have a class called 'Control' which inherits from System.Web.UI.Page, from which every other class (e.g. the aspx pages and their code behind pages) inherits. I need to maintain a list of customers etc. somewhere where it can be accessed by every user of the website (currently accessing it) and thought that this may be a good place, but if every user is accessing a different instance of the program, this list will be different for every user (as only they will be communicating with it).
If my thoughts are correct, to keep this list updated would I have to synchronize it in every instance of the program some how (possibly using threading)? Or would I have to connect to an external program which maintains this list? Or am I wrong about everything?
Thanks in advance, and sorry if this sounds like a load of nonsense; I am very confused!
Edit:
Thank you to all who have answered. I already have a database to which this data is being stored, but I also wanted to represent some of the data in the program.
I am making a booking system and have a big input form, and my plan is to load the data into objects (bookings, customers etc.) when it comes into the program (so that I don't lose the data during successive post backs), get these objects to write it to the database (it is a requirement of my client to write all data to the database as soon as it comes in to the program to minimize loss if the system goes down), and to then retain those objects software side as the program has to put constrains on what users can book (check that these services are available to them) and this would require some logic which would be easier with objects instead of having to back to the database a lot.
I therefore had the idea of storing this data in a place which was accessible to every website instance, and this is what I was confused about how to do.
It sounds like you are looking for the Cache property of the HttpContext class. The Cache shares data across the application domain, as opposed to the Items collection, which is per request. See msdn. Note that you will still need to store the data in a database as commented above.
You want to store your data in an external place like a database. Your application can then for every user load the data needed to display from the same database. Your application will grow and if you have to edit the data to a later point in time you already have all the needed pieces in place.

Non-Volatile object in C#

I want to be able to maintain a count and a last accessed date across application loads for a web service polling application. I'm not too sure what the best way to do this is. I dont like the idea of storing that data in a database as I would have to create one specifically for the purpose. What other options do I have and are there any particularly nice ways of keeping application state between subsequent runs of the app?
Persisting data eh? I suggest a database or file.
File solutions you can just XML serialize to a file and load it again when the app starts.
If the data is shared or might ever grow, then a database is probably the best solution. You can find one that fits your need among the many free projects if you wish:
couchdb
mysql
postgres
mangodb
membase
sqlite
etc
You could roll your own solution that doesn't involve a database, but most likely there is one that fits your needs and learning it would be useful beyond just the project at hand.
Don't be afraid to make a 'configuration' style table for your website, that simply has only a few rows and let's you store runtime information as needed.
Perfectly fine.

Efficient way to analyze large amounts of data?

I need to analyze tens of thousands of lines of data. The data is imported from a text file. Each line of data has eight variables. Currently, I use a class to define the data structure. As I read through the text file, I store each line object in a generic list, List.
I am wondering if I should switch to using a relational database (SQL) as I will need to analyze the data in each line of text, trying to relate it to definition terms which I also currently store in generic lists (List).
The goal is to translate a large amount of data using definitions. I want the defined data to be filterable, searchable, etc. Using a database makes more sense the more I think about it, but I would like to confirm with more experienced developers before I make the changes, yet again (I was using structs and arraylists at first).
The only drawback I can think of, is that the data does not need to be retained after it has been translated and viewed by the user. There is no need for permanent storage of data, therefore using a database might be a little overkill.
It is not absolutely necessary to go a database. It depends on the actual size of the data and the process you need to do. If you are loading the data into a List with a custom class, why not use Linq to do your querying and filtering? Something like:
var query = from foo in List<Foo>
where foo.Prop = criteriaVar
select foo;
The real question is whether the data is so large that it cannot be loaded up into memory confortably. If that is the case, then yes, a database would be much simpler.
This is not a large amount of data. I don't see any reason to involve a database in your analysis.
There IS a query language built into C# -- LINQ. The original poster currently uses a list of objects, so there is really nothing left to do. It seems to me that a database in this situation would add far more heat than light.
It sounds like what you want is a database. Sqlite supports in-memory databases (use ":memory:" as the filename). I suspect others may have an in-memory mode as well.
I was facing the same problem that you faced now while I was working on my previous company.The thing is I was looking a concrete and good solution for a lot of bar code generated files.The bar code generates a text file with thousands of records with in a single file.Manipulating and presenting the data was so difficult for me at first.Based on the records what I programmed was, I create a class that read the file and loads the data to the data table and able to save it in database. The database what I used was SQL server 2005.Then I able to manage the saved data easily and present it which way I like it.The main point is read the data from the file and save to it to the data base.If you do so you will have a lot of options to manipulate and present as the way you like it.
If you do not mind using access, here is what you can do
Attach a blank Access db as a resource
When needed, write the db out to file.
Run a CREATE TABLE statement that handles the columns of your data
Import the data into the new table
Use sql to run your calculations
OnClose, delete that access db.
You can use a program like Resourcer to load the db into a resx file
ResourceManager res = new ResourceManager( "MyProject.blank_db", this.GetType().Assembly );
byte[] b = (byte[])res.GetObject( "access.blank" );
Then use the following code to pull the resource out of the project. Take the byte array and save it to the temp location with the temp filename
"MyProject.blank_db" is the location and name of the resource file
"access.blank" is the tab given to the resource to save
If the only thing you need to do is search and replace, you may consider using sed and awk and you can do searches using grep. Of course on a Unix platform.
From your description, I think linux command line tools can handle your data very well. Using a database may unnecessarily complicate your work. If you are using windows, these tools are also available by different ways. I would recommend cygwin. The following tools may cover your task: sort, grep, cut, awk, sed, join, paste.
These unix/linux command line tools may look scary to a windows person but there are reasons for people who love them. The following are my reasons for loving them:
They allow your skill to accumulate - your knowledge to a partially tool can be helpful in different future tasks.
They allow your efforts to accumulate - the command line (or scripts) you used to finish the task can be repeated as many times as needed with different data, without human interaction.
They usually outperform the same tool you can write. If you don't believe, try to beat sort with your version for terabyte files.

.NET form that preserves text fields across sessions?

Its a simple config app with 4 checkboxes and 5 textboxes, and all values must persist across sessions.
do I have to serialize the fields and restore them by hand? I really have no idea the best way to approach this.
You could use the User settings, reading the values on load and saving on exit.
You can find info about the settings and how to retrieve and save it on runtime here.
If you are talking an ideal solution, you should think about using MVC (which win forms does NOT encourage), so that all the data you care about comes in an encapsulated, non-UI bound object (ie, the model). The UI form populates itself from the data and the app can retrieve the data when the form is torn down. If the data object implements ISerializable, then you're pretty much done.
If you're talking expedient and are absolutely sure that it will never need to grow or change (which never happens - I only do this in one-off apps), then I would scrape the form contents and write them to an appropriate place (user settings, data files, etc).
If you're talking about building something that makes this as easy as possible with never, ever, having to worry about it ever, ever, ever again, I would see about using data binding, or creating a mapping objects that understand how to map data from objects onto UI elements and out again (for example, you could subclass the main form elements to include a name field or use the initial text field to figure out a key to look up in a serializable hashtable or a property name in an serializable object).

Categories