6 Replies - 702 Views - Last Post: 10 May 2013 - 08:43 PM

#1 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Caching shared resources

Posted 10 May 2013 - 04:57 AM

Hi,

Say I have a list of N objects. Each object has, say, an image (could also be files, or even another code object). Every object does not have a unique image, so to save memory consumption, a cache class of the sort would be convenient. This image does not need to be modified in any way at run time. Being able to allocate and delete these resources (when it is no longer being used by any object) at run time would be nice.

Of course I could do something like creating a class wrapper around a map object and handle the creation and deletion of resources through that. However, unless the other objects check the wrapper class if the resource exists, that could cause problems.

I could then of course create a map of ints and maps, where the ints represent the amount of objects associated with that resource, and use it from there, which would work fine.

However, I'm more interested in knowing if there are there any developed design patterns/classes (doesn't need to be a specific language) designed to handle such a situation? Any input on the concept in general would be good too.

Thanks.

This post has been edited by Mylo: 10 May 2013 - 05:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Caching shared resources

#2 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: Caching shared resources

Posted 10 May 2013 - 05:35 AM

Create a wrapper class and use a method similar to this... (pseudo code)

Object create_object(string resource_name) {
    if (m_loaded_resources.contains(resource_name)) {
        return m_loaded_resources[resource_name];
    }
    auto obj = new Object(); // Use a smart pointer in reality
    m_loaded_resources[resource_name] = obj;
    return obj;
}



Don't make the client check if the resource exists, the wrapper should do that for you. That is a simple implementation of "lazy" loading resources and caching them.

In the real world you would want to have smart objects so once they were no longer referenced they would automatically be destroyed.

This post has been edited by stackoverflow: 10 May 2013 - 05:36 AM

Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1989
  • View blog
  • Posts: 4,135
  • Joined: 11-December 07

Re: Caching shared resources

Posted 10 May 2013 - 05:38 AM

How do you obtain the images? Are they loaded from a file, from a database or are they generated from a series of parameters?

One method would be to create a parameter object (or just a filename if that's possible) and use that as the key in a map.

Another would be to save the parameter object and generate the image on the fly.

I might be able to offer more specific advice if you let us know how the images are obtained.
Was This Post Helpful? 1
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Caching shared resources

Posted 10 May 2013 - 05:47 AM

The images would be loaded from a file in my circumstance. Additionally, the class has an enum variable determining it's identity (a shopkeeper, merchant etc) At the moment, I would be going with the map idea. It's a small project, so anything is easy to change. I just like to know about alternative methods of doing such things even if I may not use them.

Edit: Oops, didn't notice your post until now, stackoverflow, sorry. I'll definitely do that. I haven't looked at smart pointers either, (preferring to implement things myself gets in the way) so now would be a good time too, even if I use them or not, thanks.

Now if I wanted to have the wrapper class manage multiple types of data (As opposed to just Images), would it be in preference to hold mutliple maps each committed to one kind of resource, or create a class for each type of resource I need and implement some interface like IResource? I would think the interface personally as I could then completely ignore the implementation of the wrapper class.

This post has been edited by Mylo: 10 May 2013 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: Caching shared resources

Posted 10 May 2013 - 07:22 AM

I would use some kind of interface and use one map to store the resource -> object pairs.

If you want to see a good implementation of resource caching check out XNA's Content pipeline.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1989
  • View blog
  • Posts: 4,135
  • Joined: 11-December 07

Re: Caching shared resources

Posted 10 May 2013 - 09:04 AM

Quote

Additionally, the class has an enum variable determining it's identity (a shopkeeper, merchant etc)


Stop right there. If your language implements enums as integers, use a map from integer to image. If, like Java, enums are classes with a fixed number of instances, load and store the Image in the enum itself.

Quote

Now if I wanted to have the wrapper class manage multiple types of data (As opposed to just Images)


This depends. Do all the shopkeepers have the same Image, sound file and behaviour script? Store them all in the same class (if using maps) or enum. If the shopkeepers have different sound files then use separate maps.

You should have a separate map for each type to avoid casting but your language might be able to make use of generic classes.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,104
  • Joined: 05-May 12

Re: Caching shared resources

Posted 10 May 2013 - 08:43 PM

If you can borrow a copy of Game Programming Gems I, I think it contains an article about a ResourceManager which covers what others have noted above. It article talks about the design of the manager and some strategies about how to handle taking stuff off the "cache".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1