-
January 12th, 2014, 09:50 AM
#1
Caching a .txt file on page load
I am a beginner and trying to work out how to cache a .txt file with a list of words so that the file can be searched and displayed, i understand that its better from a performance point of view and data handling to pre-cache this, but is it better to do this on a certain page load if there are multiple pages or at the start of the application. also I'm having trouble working out the proper way to cache a .txt file so that the list of words from the .txt can be search when needed. But with a .txt file the process has me stumped am i setting this up wrong or is my coding just poor here is the code i running for the .txt caching.
Code:
@{
var listOfWords = Request.Form["listOfWords"];
var greeting = "Welcome";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Here in Birmingham it's " + weekDay;
var listmessage = "";
WordList words;
if (WebCache.Get("words") == null)
{
words = new WordList(Server.MapPath("wordlist.txt"));
WebCache.Set("words", words);
}
else
{
words = WebCache.Get("words");
}
if (IsPost)
{
var list = Request["text"];
listmessage = "Results List";
}
if (IsPost)
{
if (listOfWords.IsEmpty())
{
ModelState.AddError("listOfWords", "Please enter a word");
}
}
StreamReader file = new StreamReader(Server.MapPath("wordlist.txt"));
{
}
}
@{
var dataFile = Server.MapPath("Resources/wordlist.txt");
Array ShowFile = File.ReadAllLines(dataFile);
}
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<meta/ charset="utf-8"><meta/>
</head>
<body>
<p>@greetingMessage </p>
<section>
<form method="post">
<fieldset>
<legend>Enter a word to search the .txt file</legend>
<label>
Anagram:
<input type ="text" name ="listOfWords" value ="@listOfWords"/>
</label>
@Html.ValidationMessage("listOfWords")
<p><button type="submit">Search</button></p>
<p>@listmessage <p>@listOfWords</p>
</fieldset>
@Html.ValidationSummary(true)
</form>
</section>
<p>
@foreach (string dataLine in ShowFile)
{
foreach (string dataItem in dataLine.Split(','))
{
@dataItem
}
<br />
}
</p>
</body>
</html>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|