configuration.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package ccache
  2. type Configuration struct {
  3. maxSize int64
  4. buckets int
  5. itemsToPrune int
  6. deleteBuffer int
  7. promoteBuffer int
  8. getsPerPromote int32
  9. tracking bool
  10. }
  11. // Creates a configuration object with sensible defaults
  12. // Use this as the start of the fluent configuration:
  13. // e.g.: ccache.New(ccache.Configure().MaxSize(10000))
  14. func Configure() *Configuration {
  15. return &Configuration{
  16. buckets: 16,
  17. itemsToPrune: 500,
  18. deleteBuffer: 1024,
  19. getsPerPromote: 3,
  20. promoteBuffer: 1024,
  21. maxSize: 5000,
  22. tracking: false,
  23. }
  24. }
  25. // The max size for the cache
  26. // [5000]
  27. func (c *Configuration) MaxSize(max int64) *Configuration {
  28. c.maxSize = max
  29. return c
  30. }
  31. // Keys are hashed into % bucket count to provide greater concurrency (every set
  32. // requires a write lock on the bucket). Must be a power of 2 (1, 2, 4, 8, 16, ...)
  33. // [16]
  34. func (c *Configuration) Buckets(count uint32) *Configuration {
  35. if count == 0 || ((count&(^count+1)) == count) == false {
  36. count = 16
  37. }
  38. c.buckets = int(count)
  39. return c
  40. }
  41. // The number of items to prune when memory is low
  42. // [500]
  43. func (c *Configuration) ItemsToPrune(count uint32) *Configuration {
  44. c.itemsToPrune = int(count)
  45. return c
  46. }
  47. // The size of the queue for items which should be promoted. If the queue fills
  48. // up, promotions are skipped
  49. // [1024]
  50. func (c *Configuration) PromoteBuffer(size uint32) *Configuration {
  51. c.promoteBuffer = int(size)
  52. return c
  53. }
  54. // The size of the queue for items which should be deleted. If the queue fills
  55. // up, calls to Delete() will block
  56. func (c *Configuration) DeleteBuffer(size uint32) *Configuration {
  57. c.deleteBuffer = int(size)
  58. return c
  59. }
  60. // Give a large cache with a high read / write ratio, it's usually unecessary
  61. // to promote an item on every Get. GetsPerPromote specifies the number of Gets
  62. // a key must have before being promoted
  63. // [3]
  64. func (c *Configuration) GetsPerPromote(count int32) *Configuration {
  65. c.getsPerPromote = count
  66. return c
  67. }
  68. // Typically, a cache is agnostic about how cached values are use. This is fine
  69. // for a typical cache usage, where you fetch an item from the cache, do something
  70. // (write it out) and nothing else.
  71. // However, if callers are going to keep a reference to a cached item for a long
  72. // time, things get messy. Specifically, the cache can evict the item, while
  73. // references still exist. Technically, this isn't an issue. However, if you reload
  74. // the item back into the cache, you end up with 2 objects representing the same
  75. // data. This is a waste of space and could lead to weird behavior (the type an
  76. // identity map is meant to solve).
  77. // By turning tracking on and using the cache's TrackingGet, the cache
  78. // won't evict items which you haven't called Release() on. It's a simple reference
  79. // counter.
  80. func (c *Configuration) Track() *Configuration {
  81. c.tracking = true
  82. return c
  83. }