[cachebox] Eviction Policies

I am wanting to change a lot of our caches to use LFU for eviction versus LRU. However, we tend to get a lot of single hits on cache items, so what I really want to do is use the hits as the primary sort, and then last accessed time as the secondary sort. Has anyone done this or is there a way to do it without having to roll my own eviction policy?

Mary Jo

You would need to roll your own policy. It’s pretty easy to do, just start with one of the existing ones.

Think of it like sorting a database query by two different columns. (The top N records get evicted) The secondary sort only kicks in in places where there is a tie in the primary sort.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Except that all the examples have only a single sort. How do you do the secondary sort?

Mary Jo

Thats up to you! Any valid CFML code will work. :slight_smile:

Most all the policies just use the structSort function if I recall to sort the pool of metadata by one of its nested keys. You could do the same process for your primary sort, then loop over the results, duplicate each block of contiguous records with the same primary sort and apply the secondary sort to that subset. That can be optimized (short-circuited) based on how many items you are evicting each time.

If your pool size is small enough, it might just be faster to build a query object and do a QofQ against it.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Ah okay gotcha. It looks like just doing a new version of getSortedKeys() that allows for a secondary sort would work best.

Mary Jo

Just to follow up on this… I was not having much luck figuring out a reasonable way to do this that didn’t require too much manipulation and looping for my liking. So I came at the problem a totally different way. The whole reason I needed to do this was the the items I was caching tend to have a lot of variations and so end up with lots of cached data with just 1 hit. I wanted to make sure that it doesn’t evict anything with multiple hits, and also that it evicts the oldest of the “one hit wonders” and not ones that could still get used again given more time. So rather than trying to sort on hits at all, I just moved all the items with 1 hit into a new struct and sort that on the last accessed date. If I understand how this works, this should give me the results I’m looking for with these caches, since they will always have lots of these 1-hit items in the cache.

Mary Jo

That sounds like a good idea. Remember, eviction only runs when the cache fills up to its max objects. A shorter timeout may also help keep the cache clear if objects are likely to be used again soon or not at all. And remember, CacheBox has two types of timeouts: regular and “last access”. Regular timeouts are deadlines set in stone that item won’t last past. But last access timeouts can make items disappear sooner if they’re not getting hit. For instance, setting an item with a timeout of an hour but a last access timeout of 10 minutes means it CAN live in the cache for up to 60 minutes if it’s active, but if 11 minutes go by without it being used, it expires.

Generally speaking, I would recommend a healthy cache be tuned such that its size and timeouts work in conjunction to keep it within range naturally, and the eviction policy is there to help force the size down if necessary.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Oops, I remembered this right after I hit send. It might have some useful into in it.

http://blog.coldbox.org/blog/tip-of-the-week-cachebox-tuning-with-the-debug-panel

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Yup, I’ve been slowly adding more items to the caches and tweaking all the timeouts to get what I think is the best balance of settings and data in there. Right now I only have one cache that uses evictions regularly, and I am going to tweak that a bit more to reduce how much those are happening right now. I set a lot of these cache times programmatically, based on what kind of search the user is doing and whether it’s one likely to get used by anyone else, but all our searches get cached for at least some time in order to handle paging so during busy times of the day it does look like it’s piling up a bit more and evicting a lot of those before they time out. We are trying to talk the client into letting us set up a couchbase server which will allow me to cache a lot more data and longer so that we won’t see as many of those.

Mary Jo