Posts tagged Development

Caching File Contents with CacheDependency

Posted in .Net.

The CacheDependency will monitor a file or folder for changes. When it detects a change it will remove the associated key from the cache.

This comes in quite handy for my hangman game which stores the film titles in an xml file.

Below is how I cache the data and setup the CacheDependency. Some validation has been removed to keep it clear.

private HangmanGameCollection ReadGameFile()
{
    if (HttpRuntime.Cache["HangmanGame"] != null)
    {
        return (HangmanGameCollection)HttpRuntime.Cache["HangmanGame"];
    }

    HangmanGameCollection hangmanGames;

    FileInfo xmlFile = new FileInfo(Server.MapPath("~/TopSecret.xml"));

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(HangmanGameCollection));

    using (FileStream fileStream = xmlFile.OpenRead())
    {
        hangmanGames = (HangmanGameCollection)xmlSerializer.Deserialize(fileStream);

        // Put it in cache with a dependency on the xml file
        HttpRuntime.Cache.Insert("HangmanGame", hangmanGames, new CacheDependency(xmlFile.FullName));
    }

    return hangmanGames;
}

private HangmanGameCollection ReadGameFile()
{
    if (HttpRuntime.Cache["HangmanGame"] != null)
    {
        return (HangmanGameCollection)HttpRuntime.Cache["HangmanGame"];
    }

    HangmanGameCollection hangmanGames;

    FileInfo xmlFile = new FileInfo(Server.MapPath("~/TopSecret.xml"));

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(HangmanGameCollection));

    using (FileStream fileStream = xmlFile.OpenRead())
    {
        hangmanGames = (HangmanGameCollection)xmlSerializer.Deserialize(fileStream);

        // Put it in cache with a dependency on the xml file
        HttpRuntime.Cache.Insert("HangmanGame", hangmanGames, new CacheDependency(xmlFile.FullName));
    }

    return hangmanGames;
}

Should maybe do something about fitting in long lines of code :\

Tags: Development , C# , Cache , Hangman

First Post

Posted in Blog.

There is still a lot to do but I have finally gotten a working version of my blog online. I chose to write my own blog for a chance to play with asp.net MVC 3 and the Razor view engine. So far I really like the Razor view engine and think my HTML looks much cleaner when working with it.  Here is a list of what I hope to update over time:

  • Add a search feature
  • Have a correctly formatted date when viewing old posts
  • Implement MetaWeblog API
  • Comments support
  • Contact page
  • Detect tweets not loading and add redundency
  • Add a custom error page
  • Make sure all the HTML and CSS is valid
  • Minify and combine the js files
  • A few styling tweaks

Maybe add a style for lists too.

I did manage to implement an RSS feed and tags which I had missed or not wanted in my original specification.

I would like to thank Steven Gibson (@glasgowtozocalo) for doing the design for me. It's got the clean, easy to read look I wanted and I'm really happy with it.

Tags: Development , Blog

Showing 1 to 2 of 2