简体   繁体   中英

How to cache reading .csv files in C#

This may be a noob question, but I need some help. I have written two simple methods in C#: ReadCsv_IT and GetTranslation . The ReadCsv_IT method reads from a csv file. The GetTransaltion method calls the ReadCsv_IT method and returns the translated input (string key).

My problem is that in the future I will have to request a lot of times GetTranslation, but I obviously don't want to read the .csv files every time. So I was thinking about ways to use cache Memory to optimize my program, so that I don't have to read the .csv file on every request. But I am not sure how to do it and what I could do to optimize my program. Can anyone please help ?

public string ReadCsv_IT(string key)
{
    string newKey = "";

    using (var streamReader = new StreamReader(@"MyResource.csv"))
    {
        CsvReader csv = new CsvReader(streamReader);
        csv.Configuration.Delimiter = ";";

        List<DataRecord> rec = csv.GetRecords<DataRecord>().ToList();
        DataRecord record = rec.FirstOrDefault(a => a.ORIGTITLE == key);

        if (record != null)
        {
            //DOES THE LOCALIZATION with the help of the .csv file.
        }
    }
    return newKey;
}

Here is the GetTranslation Method:

public string GetTranslation(string key, string culture = null)
{
    string result = "";

    if (culture == null)
    {
        culture = Thread.CurrentThread.CurrentCulture.Name;
    }

    if (culture == "it-IT")
    {
        result = ReadCsv_IT(key);
    }

    return result;
}

Here is also the class DataRecord.

class DataRecord
{
    public string ORIGTITLE { get; set; }
    public string REPLACETITLE { get; set; }
    public string ORIGTOOLTIP { get; set; }
}

}

Two options IMO:

Turn your stream into an object? In other words: Make a class stream so you can refer to that object of the class stream.

Second:

Initialize your stream in the scope that calls for GetTranslation, and pass it on as an attribute to GetTranslation and ReadCSV_IT.

Brecht C and Thom Hubers have already given you good advice. I would like to add one more point, though: using csv files for localization in .NET is not really a good idea. Microsoft recommends using a resource-based approach ( this article is a good starting point). It seems to me that you are trying to write code for something that is already built into .NET.

From a translation point of view csv files are not the best possible format either. First of all, they are not really standardized: many tools have slightly different ways to handle commas, quotes, and line breaks that are part of the translated text. Besides, translators will be tempted to open them in Excel, and -unless handled with caution- Excel will write out translations in whatever encoding it deems best.

If the project you are working on is for learning please feel free to go ahead with it, but if you are developing software that will be used by customers, updated, translated into several target languages, and redeployed, I would recommend to reconsider your internationalization approach.

@Brecht C is right, use that answer to start. When a variable has to be cached to be used by multiple threads or instances: take a look at InMemoryCache or Redis when perfomance and distribution over several clients gets an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM