You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.1KB

  1. package cache
  2. import (
  3. "context"
  4. "time"
  5. )
  6. // Configure configure cache.
  7. type Configure func(*Cache)
  8. // WithDataOption sets cache default data options.
  9. func WithDataOption(do ...Option) Configure {
  10. return func(c *Cache) {
  11. factory := c.dataFactory
  12. c.dataFactory = func(key, value interface{}, opts ...Option) *Item {
  13. return factory(key, value, append(do, opts...)...)
  14. }
  15. }
  16. }
  17. // WithDefaultNamespace sets cache default namespace.
  18. func WithDefaultNamespace(ns, separator string) Configure {
  19. return WithDataOption(WithNamespace(ns, separator))
  20. }
  21. // WithDefaultTTL sets cache default ttl.
  22. func WithDefaultTTL(ttl time.Duration) Configure {
  23. return WithDataOption(WithTTL(ttl))
  24. }
  25. // WithHandleSet add a handler for the set operation.
  26. func WithHandleSet(m ...Handle) Configure {
  27. return WithHandleOperation(OperationSet, m...)
  28. }
  29. // WithHandleGet add a handler for the get operation.
  30. func WithHandleGet(m ...Handle) Configure {
  31. return WithHandleOperation(OperationGet, m...)
  32. }
  33. // WithHandleDelete add a handler for the delete operation.
  34. func WithHandleDelete(m ...Handle) Configure {
  35. return WithHandleOperation(OperationDelete, m...)
  36. }
  37. // WithHandleOperation add a handler for the operation.
  38. func WithHandleOperation(op string, m ...Handle) Configure {
  39. handle := ChainHandle(m...)
  40. return WithMiddleware(func(ctx context.Context, operation string, item *Item, next Provider) error {
  41. if operation == op {
  42. return handle(ctx, op, item, next)
  43. }
  44. return next(ctx, operation, item)
  45. })
  46. }
  47. // WithMiddleware sets middleware to provider.
  48. func WithMiddleware(mw ...Handle) Configure {
  49. return func(c *Cache) {
  50. prov := c.provider
  51. c.provider = chain(prov, mw...)
  52. }
  53. }
  54. // New creates new cache by provider.
  55. func New(prov Provider, opts ...Configure) *Cache {
  56. c := &Cache{
  57. provider: prov,
  58. dataFactory: NewItem,
  59. }
  60. for _, o := range opts {
  61. o(c)
  62. }
  63. return c
  64. }
  65. // Cache base cache.
  66. type Cache struct {
  67. dataFactory func(key, value interface{}, opts ...Option) *Item
  68. provider Provider
  69. }
  70. func (c *Cache) With(opts ...Configure) *Cache {
  71. cache := &Cache{
  72. provider: c.provider,
  73. dataFactory: c.dataFactory,
  74. }
  75. for _, o := range opts {
  76. o(cache)
  77. }
  78. return cache
  79. }
  80. func (c *Cache) Item(key, value interface{}, opts ...Option) *Item {
  81. return c.dataFactory(key, value, opts...)
  82. }
  83. func (c *Cache) Execute(ctx context.Context, operation string, key, value interface{}, opts ...Option) error {
  84. return c.provider(ctx, operation, c.Item(key, value, opts...))
  85. }
  86. // Set handles middlewares and sets value by key and options.
  87. func (c *Cache) Set(ctx context.Context, key, value interface{}, opts ...Option) error {
  88. return c.Execute(ctx, OperationSet, key, value, opts...)
  89. }
  90. // Get handles middlewares and gets value by key and options.
  91. func (c *Cache) Get(ctx context.Context, key, value interface{}, opts ...Option) error {
  92. return c.Execute(ctx, OperationGet, key, value, opts...)
  93. }
  94. // Delete handles middlewares and delete value by key and options.
  95. func (c *Cache) Delete(ctx context.Context, key interface{}, opts ...Option) error {
  96. return c.Execute(ctx, OperationDelete, key, nil, opts...)
  97. }