Skip to content

Commit c472650

Browse files
Refactor cache initialization to accept CacheManagerInterface for improved cache management
1 parent 60ca881 commit c472650

19 files changed

Lines changed: 161 additions & 109 deletions

backend/cmd/server/servicemanager.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"github.com/asgardeo/thunder/internal/ou"
5656
"github.com/asgardeo/thunder/internal/resource"
5757
"github.com/asgardeo/thunder/internal/role"
58+
"github.com/asgardeo/thunder/internal/system/cache"
5859
"github.com/asgardeo/thunder/internal/system/crypto/hash"
5960
"github.com/asgardeo/thunder/internal/system/crypto/pki"
6061
declarativeresource "github.com/asgardeo/thunder/internal/system/declarative_resource"
@@ -79,6 +80,7 @@ var observabilitySvc observability.ObservabilityServiceInterface
7980
// registerServices registers all the services with the provided HTTP multiplexer.
8081
func registerServices(mux *http.ServeMux) jwt.JWTServiceInterface {
8182
logger := log.GetLogger()
83+
cacheManager := cache.GetCacheManager()
8284

8385
// Load the server's private key for signing JWTs.
8486
pkiService, err := pki.Initialize()
@@ -130,14 +132,14 @@ func registerServices(mux *http.ServeMux) jwt.JWTServiceInterface {
130132

131133
// Initialize user schema service
132134
userSchemaService, userSchemaExporter, err := userschema.Initialize(
133-
mux, ouService, ouAuthzService, consentService)
135+
mux, cacheManager, ouService, ouAuthzService, consentService)
134136
if err != nil {
135137
logger.Fatal("Failed to initialize UserSchemaService", log.Error(err))
136138
}
137139
exporters = append(exporters, userSchemaExporter)
138140

139141
// Initialize entity service
140-
entityService, err := entity.Initialize(hashService, userSchemaService, ouService)
142+
entityService, err := entity.Initialize(cacheManager, hashService, userSchemaService, ouService)
141143
if err != nil {
142144
logger.Fatal("Failed to initialize EntityService", log.Error(err))
143145
}
@@ -232,7 +234,7 @@ func registerServices(mux *http.ServeMux) jwt.JWTServiceInterface {
232234
attributeCacheService := attributecache.Initialize()
233235

234236
// Initialize flow and executor services.
235-
flowFactory, graphCache := flowcore.Initialize()
237+
flowFactory, graphCache := flowcore.Initialize(cacheManager)
236238
var emailClient email.EmailClientInterface
237239
emailClient, err = email.Initialize()
238240
if err != nil {
@@ -245,12 +247,13 @@ func registerServices(mux *http.ServeMux) jwt.JWTServiceInterface {
245247
observabilitySvc, groupService, roleService, entityProvider, attributeCacheService, emailClient,
246248
templateService, oauthAuthnService, oidcAuthnService, githubAuthnService, googleAuthnService)
247249

248-
flowMgtService, flowMgtExporter, err := flowmgt.Initialize(mux, mcpServer, flowFactory, execRegistry, graphCache)
250+
flowMgtService, flowMgtExporter, err := flowmgt.Initialize(
251+
mux, mcpServer, cacheManager, flowFactory, execRegistry, graphCache)
249252
if err != nil {
250253
logger.Fatal("Failed to initialize FlowMgtService", log.Error(err))
251254
}
252255
exporters = append(exporters, flowMgtExporter)
253-
certservice, err := cert.Initialize()
256+
certservice, err := cert.Initialize(cacheManager)
254257
if err != nil {
255258
logger.Fatal("Failed to initialize CertificateService", log.Error(err))
256259
}
@@ -270,8 +273,8 @@ func registerServices(mux *http.ServeMux) jwt.JWTServiceInterface {
270273

271274
// TODO: Remove entityService dependency after finalizing declarative resource loading pattern
272275
applicationService, applicationExporter, err := application.Initialize(
273-
mux, mcpServer, entityProvider, entityService, ouService, certservice, flowMgtService, themeMgtService,
274-
layoutMgtService, userSchemaService, consentService)
276+
mux, mcpServer, cacheManager, entityProvider, entityService, ouService, certservice,
277+
flowMgtService, themeMgtService, layoutMgtService, userSchemaService, consentService)
275278
if err != nil {
276279
logger.Fatal("Failed to initialize ApplicationService", log.Error(err))
277280
}

backend/internal/application/cache_backed_store.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ type cachedBackedApplicationStore struct {
3434
}
3535

3636
// newCachedBackedApplicationStore creates a new cache-backed application store.
37-
func newCachedBackedApplicationStore(store applicationStoreInterface) applicationStoreInterface {
37+
func newCachedBackedApplicationStore(store applicationStoreInterface,
38+
appByIDCache cache.CacheInterface[*applicationConfigDAO],
39+
oauthAppCache cache.CacheInterface[*oauthConfigDAO]) applicationStoreInterface {
3840
return &cachedBackedApplicationStore{
39-
AppByIDCache: cache.GetCache[*applicationConfigDAO]("ApplicationByIDCache"),
40-
OAuthAppCache: cache.GetCache[*oauthConfigDAO]("OAuthAppByEntityIDCache"),
41+
AppByIDCache: appByIDCache,
42+
OAuthAppCache: oauthAppCache,
4143
Store: store,
4244
}
4345
}

backend/internal/application/init.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/asgardeo/thunder/internal/entityprovider"
3333
flowmgt "github.com/asgardeo/thunder/internal/flow/mgt"
3434
oupkg "github.com/asgardeo/thunder/internal/ou"
35+
"github.com/asgardeo/thunder/internal/system/cache"
3536
serverconst "github.com/asgardeo/thunder/internal/system/constants"
3637
declarativeresource "github.com/asgardeo/thunder/internal/system/declarative_resource"
3738
"github.com/asgardeo/thunder/internal/system/middleware"
@@ -43,6 +44,7 @@ import (
4344
func Initialize(
4445
mux *http.ServeMux,
4546
mcpServer *mcp.Server,
47+
cacheManager cache.CacheManagerInterface,
4648
entityProvider entityprovider.EntityProviderInterface,
4749
entityService entity.EntityServiceInterface,
4850
ouService oupkg.OrganizationUnitServiceInterface,
@@ -54,7 +56,7 @@ func Initialize(
5456
consentService consent.ConsentServiceInterface,
5557
) (ApplicationServiceInterface, declarativeresource.ResourceExporter, error) {
5658
// Step 1: Initialize store and transactioner based on store mode
57-
appStore, transactioner, err := initializeStore()
59+
appStore, transactioner, err := initializeStore(cacheManager)
5860
if err != nil {
5961
return nil, nil, err
6062
}
@@ -123,7 +125,8 @@ func Initialize(
123125
// - If application.store is not specified, falls back to global declarative_resources.enabled:
124126
// - If declarative_resources.enabled = true: behaves as IMMUTABLE mode
125127
// - If declarative_resources.enabled = false: behaves as MUTABLE mode
126-
func initializeStore() (applicationStoreInterface, transaction.Transactioner, error) {
128+
func initializeStore(cacheManager cache.CacheManagerInterface) (
129+
applicationStoreInterface, transaction.Transactioner, error) {
127130
storeMode := getApplicationStoreMode()
128131

129132
switch storeMode {
@@ -144,7 +147,9 @@ func initializeStore() (applicationStoreInterface, transaction.Transactioner, er
144147
if err != nil {
145148
return nil, nil, err
146149
}
147-
return newCachedBackedApplicationStore(dbStore), transactioner, nil
150+
appByIDCache := cache.GetCache[*applicationConfigDAO](cacheManager, "ApplicationByIDCache")
151+
oauthAppCache := cache.GetCache[*oauthConfigDAO](cacheManager, "OAuthAppByEntityIDCache")
152+
return newCachedBackedApplicationStore(dbStore, appByIDCache, oauthAppCache), transactioner, nil
148153
}
149154
}
150155

backend/internal/application/init_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/asgardeo/thunder/internal/application/model"
3030
"github.com/asgardeo/thunder/internal/cert"
3131
oauth2const "github.com/asgardeo/thunder/internal/oauth/oauth2/constants"
32+
"github.com/asgardeo/thunder/internal/system/cache"
3233
"github.com/asgardeo/thunder/internal/system/config"
3334
dbmodel "github.com/asgardeo/thunder/internal/system/database/model"
3435
"github.com/asgardeo/thunder/internal/system/database/provider"
@@ -156,6 +157,7 @@ func (suite *InitTestSuite) TestInitialize_WithDeclarativeResourcesDisabled() {
156157
service, _, err := Initialize(
157158
mux,
158159
nil,
160+
cache.GetCacheManager(),
159161
nil, // entityProvider - not needed for this test
160162
mockEntityService,
161163
nil, // ouService - not needed for this test
@@ -202,6 +204,7 @@ func (suite *InitTestSuite) TestInitialize_WithMCPServer() {
202204
service, _, err := Initialize(
203205
mux,
204206
mcpServer,
207+
cache.GetCacheManager(),
205208
nil, // entityProvider - not needed for this test
206209
mockEntityService,
207210
nil, // ouService - not needed for this test
@@ -605,6 +608,7 @@ func TestInitialize_Standalone(t *testing.T) {
605608
service, _, err := Initialize(
606609
mux,
607610
nil,
611+
cache.GetCacheManager(),
608612
nil, // entityProvider - not needed for this test
609613
mockEntityService,
610614
nil, // ouService - not needed for this test
@@ -660,6 +664,7 @@ func TestInitialize_WithDeclarativeResources_Standalone(t *testing.T) {
660664
service, _, err := Initialize(
661665
mux,
662666
nil,
667+
cache.GetCacheManager(),
663668
nil, // entityProvider - not needed for this test
664669
mockEntityService,
665670
nil, // ouService - not needed for this test

backend/internal/cert/cache_backed_store.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ type cacheBackedStore struct {
3636
}
3737

3838
// NewCachedBackedCertificateStore creates a new instance of CachedBackedCertificateStore.
39-
func newCachedBackedCertificateStore() certificateStoreInterface {
39+
func newCachedBackedCertificateStore(
40+
certByIDCache cache.CacheInterface[*Certificate],
41+
certByReferenceCache cache.CacheInterface[*Certificate]) certificateStoreInterface {
4042
return &cacheBackedStore{
41-
certByIDCache: cache.GetCache[*Certificate]("CertificateByIDCache"),
42-
certByReferenceCache: cache.GetCache[*Certificate]("CertificateByReferenceCache"),
43+
certByIDCache: certByIDCache,
44+
certByReferenceCache: certByReferenceCache,
4345
store: newCertificateStore(),
4446
}
4547
}

backend/internal/cert/init.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@
1919
package cert
2020

2121
import (
22+
"github.com/asgardeo/thunder/internal/system/cache"
2223
"github.com/asgardeo/thunder/internal/system/database/provider"
2324
)
2425

2526
// Initialize initializes and returns the certificate service.
26-
func Initialize() (CertificateServiceInterface, error) {
27+
func Initialize(cacheManager cache.CacheManagerInterface) (CertificateServiceInterface, error) {
2728
dbProvider := provider.GetDBProvider()
2829
txn, err := dbProvider.GetConfigDBTransactioner()
2930
if err != nil {
3031
return nil, err
3132
}
32-
certStore := newCachedBackedCertificateStore()
33+
certByIDCache := cache.GetCache[*Certificate](cacheManager, "CertificateByIDCache")
34+
certByReferenceCache := cache.GetCache[*Certificate](cacheManager, "CertificateByReferenceCache")
35+
certStore := newCachedBackedCertificateStore(certByIDCache, certByReferenceCache)
3336
certService := newCertificateService(certStore, txn)
3437
return certService, nil
3538
}

backend/internal/entity/cache_backed_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type cacheBackedEntityStore struct {
3535
}
3636

3737
// newCacheBackedEntityStore creates a cache-backed wrapper around the given store.
38-
func newCacheBackedEntityStore(store entityStoreInterface) entityStoreInterface {
38+
func newCacheBackedEntityStore(store entityStoreInterface,
39+
entityByIDCache cache.CacheInterface[*Entity]) entityStoreInterface {
3940
return &cacheBackedEntityStore{
40-
entityByIDCache: cache.GetCache[*Entity]("EntityByIDCache"),
41+
entityByIDCache: entityByIDCache,
4142
store: store,
4243
logger: log.GetLogger().With(
4344
log.String(log.LoggerKeyComponentName, "CacheBackedEntityStore")),

backend/internal/entity/init.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package entity
2020

2121
import (
2222
"github.com/asgardeo/thunder/internal/ou"
23+
"github.com/asgardeo/thunder/internal/system/cache"
2324
"github.com/asgardeo/thunder/internal/system/crypto/hash"
2425
"github.com/asgardeo/thunder/internal/system/transaction"
2526
"github.com/asgardeo/thunder/internal/userschema"
@@ -30,11 +31,12 @@ import (
3031
// Declarative resources are loaded on demand by consumer packages (e.g. user, application)
3132
// based on their own store mode configuration.
3233
func Initialize(
34+
cacheManager cache.CacheManagerInterface,
3335
hashService hash.HashServiceInterface,
3436
userSchemaService userschema.UserSchemaServiceInterface,
3537
ouService ou.OrganizationUnitServiceInterface,
3638
) (EntityServiceInterface, error) {
37-
store, transactioner, err := initializeStore()
39+
store, transactioner, err := initializeStore(cacheManager)
3840
if err != nil {
3941
return nil, err
4042
}
@@ -44,12 +46,14 @@ func Initialize(
4446
}
4547

4648
// initializeStore always creates a composite store (DB + in-memory file store).
47-
func initializeStore() (entityStoreInterface, transaction.Transactioner, error) {
49+
func initializeStore(cacheManager cache.CacheManagerInterface) (
50+
entityStoreInterface, transaction.Transactioner, error) {
4851
fileStore := newEntityFileBasedStore()
4952
dbStore, transactioner, err := newEntityDBStore()
5053
if err != nil {
5154
return nil, nil, err
5255
}
53-
cacheBackedEntityStore := newCacheBackedEntityStore(dbStore)
56+
entityByIDCache := cache.GetCache[*Entity](cacheManager, "EntityByIDCache")
57+
cacheBackedEntityStore := newCacheBackedEntityStore(dbStore, entityByIDCache)
5458
return newEntityCompositeStore(fileStore, cacheBackedEntityStore), transactioner, nil
5559
}

backend/internal/flow/core/graph_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ type graphCache struct {
3838
}
3939

4040
// newGraphCache creates a new in-memory cache instance of graphCache.
41-
func newGraphCache() GraphCacheInterface {
41+
func newGraphCache(c cache.CacheInterface[*graph]) GraphCacheInterface {
4242
return &graphCache{
43-
cache: cache.GetInMemoryCache[*graph]("FlowGraphCache"),
43+
cache: c,
4444
}
4545
}
4646

backend/internal/flow/core/init.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
// Package core provides the core structs for flow management and execution.
2020
package core
2121

22+
import "github.com/asgardeo/thunder/internal/system/cache"
23+
2224
// Initialize initializes the core flow package
23-
func Initialize() (FlowFactoryInterface, GraphCacheInterface) {
25+
func Initialize(cacheManager cache.CacheManagerInterface) (FlowFactoryInterface, GraphCacheInterface) {
2426
flowFactory := newFlowFactory()
25-
graphCache := newGraphCache()
27+
graphCacheInst := cache.GetInMemoryCache[*graph](cacheManager, "FlowGraphCache")
28+
graphCache := newGraphCache(graphCacheInst)
2629
return flowFactory, graphCache
2730
}

0 commit comments

Comments
 (0)