Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

This is a "Least Recently Used Cache", that has a maximum size, and when the size is exceeded, it deletes the least recently used entries.

Warnings & Limitations

nodeId is kept as a ushort. When the maximum nodeId is too large, the entire cache is renumbered. This causes use history to be lost at that point. I estimate that this will be infrequent, and not a significant loss of efficiency, but it can result in more recently used members being forgotten before less frequently used members.

Don't put too many items in a cache. This isn't efficient. I don't yet know what a good size would be, but I don't expect to ever use more than around 1,000. Otherwise some of the design decisions would have been made differently.

Note that a cache entails overhead on each access to an item. It's not much, but it does add up. This is a quick-access place to store things that you have a slow way to get to that isn't so forgetful. (Think records on a disk file. If they're still in memory, it's faster to get them from the cache. If not, you read them again from the disk.)

If anyone has a FAST way to renumber the cache without losing historical access patterns, I'd like to hear it.

This code is released under the MIT license. If you want something else, ask.

source:trunk/lrucache

Attachments

  • lrucache2.d (4.7 kB) -LRUCache source code, added by CharlesH on 11/10/07 18:19:48.
  • lrucache.d (4.7 kB) -lrucache.d (tabs = 3 spaces if you need to expand), added by CharlesH on 11/10/07 18:39:15.