4

Is a class such as this necessary?

public class ContentCache
{
    private readonly ContentManager _content;
    private readonly Dictionary<string, Texture2D> _textureCache = new Dictionary<string, Texture2D>();

    public ContentCache(ContentManager content)
    {
        _content = content;
    }

    public Texture2D Load(string assetName)
    {
        Texture2D texture = null;
        if (!_textureCache.TryGetValue(assetName, out texture))
        {
            _textureCache[assetName] =
                texture = _content.Load<Texture2D>(assetName);
        }
        return texture;
    }
}

I am curious if ContentManager.Load<Texture2D>() does it's own caching internally. I don't want to double-cache things.

Note:

Our XNA game is 2D and going to run on WP7 and Windows, and also iOS and OSX using MonoGame.

MonoGame may function differently than XNA in Windows, but I can probably browse it's source to find that out.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • 1
    This was a great question. It turns out I have been writing a class nearly identical to your for the same reasons. Nice to know it isn't needed any more according to the answers you got. – Bradley Uffner Oct 20 '11 at 23:03

3 Answers3

4

The class is unnecessary. ContentManager does this on your behalf.

Source:

http://forums.create.msdn.com/forums/p/31383/178975.aspx

Note:

As far as Mono goes... I'm sure the implementations mirror each other quite well, but I can't be certain on this occasion.

Also, if you WANT to re-load an asset, you could use an additional ContentManager and throw it away afterward.

cwharris
  • 17,835
  • 4
  • 44
  • 64
  • We may keep it around depending what MonoGame does. I'll inspect the source on it and post-back. – jonathanpeppers Oct 20 '11 at 23:20
  • 1
    MonoGame doesn't appear to cache any content files: https://github.com/mono/MonoGame/blob/master/MonoGame.Framework/Content/ContentManager.cs So I think we are going to use this class and some `#if MONOGAME` preprocessor statements, and not use caching on regular XNA. – jonathanpeppers Oct 20 '11 at 23:29
  • MonoGame most definitely SHOULD cache the assets in the same way as the Xna framework. If it still does not, why not implement it in ContentManager where it should be. See this thread for some bare bones code: http://monogame.codeplex.com/discussions/273478 – Aranda Nov 17 '11 at 03:32
  • The linked source is no longer valid and needs to be updated. – itsme86 Nov 20 '14 at 20:34
1

It should be noted that Mono class ContentManager now also does caching.

Added sometime in 2012. For future reference.

bodge
  • 31
  • 3
0

Just cache what needs to be cache in the first LoadContent method, by using some precache string array, like:

// Preload assets
static readonly string[] preloadAssets =
{
     "Textures\\texture1",
};

protected override void LoadContent()
{
    foreach ( string asset in preloadAssets )
    {
        Content.Load<object>(asset);
    }
}

Something like that mayhaps!

Oyvind Andersson
  • 362
  • 6
  • 22
  • This is not the intent of my question or class. I think you are missing the question a bit. My class is lazy-loading textures and caching them if they are requested again. – jonathanpeppers Oct 20 '11 at 23:22