views:

30

answers:

2

I am having trouble managing cache keys in my project.

Currently we use a define-at-wish strategy and uniqueness of keys are not guaranteed.

Every time some one want to cache a kind of object, a constant is defined in the module in a form like this:

BOOK_NOTE_KEY = "BKNT%d"

An BookNote object's cache key is like BOOK_NOTE_KEY % note.id.

I think this is not a good way, because cache keys are not manageable.

What strategy do you take in your projects?

A: 

You could use the full class name as a prefix/postfix. e.g. "YourNamespace.SubNamespace.BookNote.%d"

Or you could just generate a GUID for every class and use that as a prefix/postfix.

pgroke
A: 

If you keep track of the last update time for each record (arguably a good practice anyway), then you can compose cache keys like:

BookNote/[id]-[last_updated]

tfe