|
| 1 | +// SPDX-License-Identifier: EUPL-1.2 |
| 2 | + |
| 3 | +// Service registration for the cache package. Exposes the Cache surface |
| 4 | +// as a Core service with action handlers so consumers can wire cache |
| 5 | +// operations through the same plumbing as every other core service. |
| 6 | +// |
| 7 | +// Usage example: `c, _ := core.New(core.WithName("cache", cache.NewService(cache.CacheConfig{BaseDir: "/var/lib/core/cache", TTL: time.Hour})))` |
| 8 | + |
| 9 | +package cache |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "time" |
| 14 | + |
| 15 | + core "dappco.re/go" |
| 16 | + coreio "dappco.re/go/io" |
| 17 | +) |
| 18 | + |
| 19 | +// CacheConfig is the typed-options struct for the cache service. Empty |
| 20 | +// values fall back to package defaults (`coreio.Local` medium, CWD-rooted |
| 21 | +// `.core/cache` baseDir, `DefaultTTL` cacheTTL). |
| 22 | +// |
| 23 | +// Usage example: `cfg := cache.CacheConfig{BaseDir: "/var/lib/core/cache", TTL: time.Hour}` |
| 24 | +type CacheConfig struct { |
| 25 | + // Medium is the storage backend. Nil → coreio.Local. |
| 26 | + Medium coreio.Medium |
| 27 | + // BaseDir is the root directory. Empty → CWD/.core/cache. |
| 28 | + BaseDir string |
| 29 | + // TTL is the default cache TTL. Zero → DefaultTTL (1 hour). |
| 30 | + TTL time.Duration |
| 31 | +} |
| 32 | + |
| 33 | +// Service is the registerable handle for the cache package — embeds |
| 34 | +// *core.ServiceRuntime[CacheConfig] for typed options access and holds |
| 35 | +// a live *Cache ready for direct method calls or action use. |
| 36 | +// |
| 37 | +// Usage example: `svc := core.MustServiceFor[*cache.Service](c, "cache"); _ = svc.Cache.Delete("key")` |
| 38 | +type Service struct { |
| 39 | + *core.ServiceRuntime[CacheConfig] |
| 40 | + // Cache is the live *Cache the service was constructed with. |
| 41 | + // Usage example: `svc.Cache.Delete("key")` |
| 42 | + Cache *Cache |
| 43 | + registrations core.Once |
| 44 | +} |
| 45 | + |
| 46 | +// NewService returns a factory that constructs the cache and produces a |
| 47 | +// *Service ready for c.Service() registration. Use through core.WithName |
| 48 | +// so the framework wires lifecycle (OnStartup registers actions). |
| 49 | +// |
| 50 | +// Usage example: `c, _ := core.New(core.WithName("cache", cache.NewService(cache.CacheConfig{BaseDir: "/var/lib/core/cache"})))` |
| 51 | +func NewService(config CacheConfig) func(*core.Core) core.Result { |
| 52 | + return func(c *core.Core) core.Result { |
| 53 | + r := New(config.Medium, config.BaseDir, config.TTL) |
| 54 | + if !r.OK { |
| 55 | + return r |
| 56 | + } |
| 57 | + return core.Ok(&Service{ |
| 58 | + ServiceRuntime: core.NewServiceRuntime(c, config), |
| 59 | + Cache: r.Value.(*Cache), |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// OnStartup registers the cache action handlers on the attached Core. |
| 65 | +// Implements core.Startable. Idempotent via core.Once — multiple startups |
| 66 | +// (e.g. test re-entry) won't double-register. |
| 67 | +// |
| 68 | +// Note: Get / Set / SetBinary / GetBinary stay direct method calls because |
| 69 | +// they need a typed `dest any` argument that doesn't round-trip through |
| 70 | +// Options cleanly. Other operations are exposed as actions. |
| 71 | +// |
| 72 | +// Usage example: `r := svc.OnStartup(ctx)` |
| 73 | +func (s *Service) OnStartup(context.Context) core.Result { |
| 74 | + if s == nil { |
| 75 | + return core.Ok(nil) |
| 76 | + } |
| 77 | + s.registrations.Do(func() { |
| 78 | + c := s.Core() |
| 79 | + if c == nil { |
| 80 | + return |
| 81 | + } |
| 82 | + c.Action("cache.delete", s.handleDelete) |
| 83 | + c.Action("cache.delete_many", s.handleDeleteMany) |
| 84 | + c.Action("cache.path", s.handlePath) |
| 85 | + c.Action("cache.invalidate", s.handleInvalidate) |
| 86 | + c.Action("cache.clear_scope", s.handleClearScope) |
| 87 | + }) |
| 88 | + return core.Ok(nil) |
| 89 | +} |
| 90 | + |
| 91 | +// OnShutdown is a no-op for the cache service — the Cache holds no |
| 92 | +// long-lived handles requiring teardown. Implements core.Stoppable for |
| 93 | +// shape parity with other services. |
| 94 | +// |
| 95 | +// Usage example: `r := svc.OnShutdown(ctx)` |
| 96 | +func (s *Service) OnShutdown(context.Context) core.Result { |
| 97 | + return core.Ok(nil) |
| 98 | +} |
| 99 | + |
| 100 | +// handleDelete — `cache.delete` action handler. Reads opts.key. |
| 101 | +// |
| 102 | +// r := c.Action("cache.delete").Run(ctx, core.NewOptions( |
| 103 | +// core.Option{Key: "key", Value: "user.profile.42"}, |
| 104 | +// )) |
| 105 | +func (s *Service) handleDelete(_ core.Context, opts core.Options) core.Result { |
| 106 | + if s == nil || s.Cache == nil { |
| 107 | + return core.Fail(core.E("cache.delete", "service not initialised", nil)) |
| 108 | + } |
| 109 | + return s.Cache.Delete(opts.String("key")) |
| 110 | +} |
| 111 | + |
| 112 | +// handleDeleteMany — `cache.delete_many` action handler. Reads |
| 113 | +// opts.keys (string slice). Removes every supplied key in one pass and |
| 114 | +// returns a count of successful deletions in r.Value. |
| 115 | +// |
| 116 | +// r := c.Action("cache.delete_many").Run(ctx, core.NewOptions( |
| 117 | +// core.Option{Key: "keys", Value: []string{"a", "b", "c"}}, |
| 118 | +// )) |
| 119 | +func (s *Service) handleDeleteMany(_ core.Context, opts core.Options) core.Result { |
| 120 | + if s == nil || s.Cache == nil { |
| 121 | + return core.Fail(core.E("cache.delete_many", "service not initialised", nil)) |
| 122 | + } |
| 123 | + r := opts.Get("keys") |
| 124 | + if !r.OK { |
| 125 | + return core.Fail(core.E("cache.delete_many", "keys is required", nil)) |
| 126 | + } |
| 127 | + keys, ok := r.Value.([]string) |
| 128 | + if !ok { |
| 129 | + return core.Fail(core.E("cache.delete_many", "keys must be []string", nil)) |
| 130 | + } |
| 131 | + return s.Cache.DeleteMany(keys...) |
| 132 | +} |
| 133 | + |
| 134 | +// handlePath — `cache.path` action handler. Reads opts.key and returns |
| 135 | +// the on-disk JSON path in r.Value. |
| 136 | +// |
| 137 | +// r := c.Action("cache.path").Run(ctx, core.NewOptions( |
| 138 | +// core.Option{Key: "key", Value: "user.profile.42"}, |
| 139 | +// )) |
| 140 | +// path, _ := r.Value.(string) |
| 141 | +func (s *Service) handlePath(_ core.Context, opts core.Options) core.Result { |
| 142 | + if s == nil || s.Cache == nil { |
| 143 | + return core.Fail(core.E("cache.path", "service not initialised", nil)) |
| 144 | + } |
| 145 | + return s.Cache.Path(opts.String("key")) |
| 146 | +} |
| 147 | + |
| 148 | +// handleInvalidate — `cache.invalidate` action handler. Reads |
| 149 | +// opts.trigger and runs every InvalidateFunc registered for that trigger. |
| 150 | +// |
| 151 | +// r := c.Action("cache.invalidate").Run(ctx, core.NewOptions( |
| 152 | +// core.Option{Key: "trigger", Value: "user.updated"}, |
| 153 | +// )) |
| 154 | +func (s *Service) handleInvalidate(_ core.Context, opts core.Options) core.Result { |
| 155 | + if s == nil || s.Cache == nil { |
| 156 | + return core.Fail(core.E("cache.invalidate", "service not initialised", nil)) |
| 157 | + } |
| 158 | + return s.Cache.Invalidate(opts.String("trigger")) |
| 159 | +} |
| 160 | + |
| 161 | +// handleClearScope — `cache.clear_scope` action handler. Reads |
| 162 | +// opts.origin and removes every entry under that scope. |
| 163 | +// |
| 164 | +// r := c.Action("cache.clear_scope").Run(ctx, core.NewOptions( |
| 165 | +// core.Option{Key: "origin", Value: "users"}, |
| 166 | +// )) |
| 167 | +func (s *Service) handleClearScope(_ core.Context, opts core.Options) core.Result { |
| 168 | + if s == nil || s.Cache == nil { |
| 169 | + return core.Fail(core.E("cache.clear_scope", "service not initialised", nil)) |
| 170 | + } |
| 171 | + return s.Cache.ClearScope(opts.String("origin")) |
| 172 | +} |
0 commit comments