@@ -52,7 +52,7 @@ type Client struct {
5252 client * mongo.Client
5353
5454 cacheManager * cache.Manager
55- projectCache * cache. LRUWithExpires [ string , * database. ProjectInfo ]
55+ projectCache * ProjectCache
5656 clientCache * cache.LRU [types.ClientRefKey , * database.ClientInfo ]
5757 docCache * cache.LRU [types.DocRefKey , * database.DocInfo ]
5858 changeCache * cache.LRU [types.DocRefKey , * ChangeStore ]
@@ -104,11 +104,7 @@ func Dial(conf *Config) (*Client, error) {
104104
105105 cacheManager := cache .NewManager (conf .ParseCacheStatsInterval ())
106106
107- projectCache , err := cache .NewLRUWithExpires [string , * database.ProjectInfo ](
108- conf .ProjectCacheSize ,
109- conf .ParseProjectCacheTTL (),
110- "projects" ,
111- )
107+ projectCache , err := NewProjectCache (conf .ProjectCacheSize , conf .ParseProjectCacheTTL ())
112108 if err != nil {
113109 return nil , fmt .Errorf ("initialize project cache: %w" , err )
114110 }
@@ -152,8 +148,7 @@ func Dial(conf *Config) (*Client, error) {
152148 config : conf ,
153149 client : client ,
154150
155- cacheManager : cacheManager ,
156-
151+ cacheManager : cacheManager ,
157152 projectCache : projectCache ,
158153 clientCache : clientCache ,
159154 docCache : docCache ,
@@ -191,7 +186,9 @@ func (c *Client) Close() error {
191186func (c * Client ) InvalidateCache (cacheType types.CacheType , key string ) {
192187 switch cacheType {
193188 case types .CacheTypeProject :
194- c .projectCache .Remove (key )
189+ if id := types .ID (key ); id .Validate () == nil {
190+ c .projectCache .Remove (id )
191+ }
195192 }
196193}
197194
@@ -566,7 +563,7 @@ func (c *Client) ListProjectInfos(
566563
567564// FindProjectInfoByPublicKey returns a project by public key.
568565func (c * Client ) FindProjectInfoByPublicKey (ctx context.Context , publicKey string ) (* database.ProjectInfo , error ) {
569- if cached , ok := c .projectCache .Get (publicKey ); ok {
566+ if cached , ok := c .projectCache .GetByAPIKey (publicKey ); ok {
570567 return cached .DeepCopy (), nil
571568 }
572569
@@ -583,7 +580,7 @@ func (c *Client) FindProjectInfoByPublicKey(ctx context.Context, publicKey strin
583580 return nil , fmt .Errorf ("find project by public key %s: %w" , publicKey , err )
584581 }
585582
586- c .projectCache .Add (publicKey , info . DeepCopy () )
583+ c .projectCache .Add (info )
587584
588585 return info , nil
589586}
@@ -631,6 +628,10 @@ func (c *Client) FindProjectInfoByName(
631628
632629// FindProjectInfoByID returns a project by the given id.
633630func (c * Client ) FindProjectInfoByID (ctx context.Context , id types.ID ) (* database.ProjectInfo , error ) {
631+ if cached , ok := c .projectCache .GetByID (id ); ok {
632+ return cached .DeepCopy (), nil
633+ }
634+
634635 result := c .collection (ColProjects ).FindOne (ctx , bson.M {
635636 "_id" : id ,
636637 })
@@ -643,6 +644,8 @@ func (c *Client) FindProjectInfoByID(ctx context.Context, id types.ID) (*databas
643644 return nil , fmt .Errorf ("find project by id %s: %w" , id , err )
644645 }
645646
647+ c .projectCache .Add (info )
648+
646649 return info , nil
647650}
648651
@@ -682,6 +685,8 @@ func (c *Client) UpdateProjectInfo(
682685 return nil , fmt .Errorf ("decode project: %w" , err )
683686 }
684687
688+ c .projectCache .Remove (info .ID )
689+
685690 return info , nil
686691}
687692
@@ -697,7 +702,7 @@ func (c *Client) RotateProjectKeys(
697702 prevInfo := & database.ProjectInfo {}
698703 res := c .collection (ColProjects ).FindOne (ctx , bson.M {"_id" : id , "owner" : owner })
699704 if err := res .Decode (prevInfo ); err == nil {
700- c .projectCache .Remove (prevInfo .PublicKey )
705+ c .projectCache .Remove (prevInfo .ID )
701706 }
702707
703708 res = c .collection (ColProjects ).FindOneAndUpdate (ctx , bson.M {
0 commit comments