@@ -21,6 +21,7 @@ import (
2121 "time"
2222
2323 "github.com/codeGROOVE-dev/bdcache"
24+ "github.com/codeGROOVE-dev/bdcache/persist/cloudrun"
2425 "github.com/codeGROOVE-dev/gsm"
2526 "github.com/codeGROOVE-dev/prcost/pkg/cost"
2627 "github.com/codeGROOVE-dev/prcost/pkg/github"
@@ -172,11 +173,22 @@ func New() *Server {
172173
173174 logger .InfoContext (ctx , "Server initialized with CSRF protection enabled" )
174175
176+ // Get database name from environment, default to "prcost" if not set.
177+ dbName := os .Getenv ("DATASTORE_DB" )
178+ if dbName == "" {
179+ dbName = "prcost"
180+ }
181+
175182 // Initialize caches with bdcache (automatically handles memory + persistence).
176- // WithBestStore uses Cloud Datastore when K_SERVICE is set (Cloud Run/Knative), local files otherwise.
183+ // cloudrun.New uses Cloud Datastore when K_SERVICE is set (Cloud Run/Knative), local files otherwise.
177184 // Single unified cache with 72-hour TTL for all GitHub query results.
185+ githubPersist , err := cloudrun .New [string , any ](ctx , dbName )
186+ if err != nil {
187+ logger .ErrorContext (ctx , "Failed to initialize GitHub cache persistence" , "error" , err )
188+ return nil
189+ }
178190 githubCache , err := bdcache .New [string , any ](ctx ,
179- bdcache .WithBestStore ( "prcost-github" ),
191+ bdcache .WithPersistence ( githubPersist ),
180192 bdcache .WithDefaultTTL (72 * time .Hour ),
181193 bdcache .WithMemorySize (2000 ),
182194 )
@@ -186,8 +198,13 @@ func New() *Server {
186198 }
187199
188200 // Separate caches for non-GitHub data with 6-day TTL
201+ prDataPersist , err := cloudrun .New [string , cost.PRData ](ctx , dbName )
202+ if err != nil {
203+ logger .ErrorContext (ctx , "Failed to initialize PR data cache persistence" , "error" , err )
204+ return nil
205+ }
189206 prDataCache , err := bdcache .New [string , cost.PRData ](ctx ,
190- bdcache .WithBestStore ( "prcost-prdata" ),
207+ bdcache .WithPersistence ( prDataPersist ),
191208 bdcache .WithDefaultTTL (6 * 24 * time .Hour ),
192209 bdcache .WithMemorySize (1000 ),
193210 )
@@ -196,8 +213,13 @@ func New() *Server {
196213 return nil
197214 }
198215
216+ calcResultPersist , err := cloudrun .New [string , cost.Breakdown ](ctx , dbName )
217+ if err != nil {
218+ logger .ErrorContext (ctx , "Failed to initialize calc result cache persistence" , "error" , err )
219+ return nil
220+ }
199221 calcResultCache , err := bdcache .New [string , cost.Breakdown ](ctx ,
200- bdcache .WithBestStore ( "prcost-calcresult" ),
222+ bdcache .WithPersistence ( calcResultPersist ),
201223 bdcache .WithDefaultTTL (6 * 24 * time .Hour ),
202224 bdcache .WithMemorySize (1000 ),
203225 )
0 commit comments