@@ -39,10 +39,19 @@ type FileStore struct {
3939 db * sql.DB
4040 initTx types.Transaction // This is the transaction for the initial setup of the app, before it is committed to the database.
4141 // After app is committed to database, this is not used, auto-commit transactions are used for reads
42+ fileCache * FileCache
4243}
4344
44- func NewFileStore (appId types.AppId , version int , metadata * Metadata , tx types.Transaction ) * FileStore {
45- return & FileStore {appId : appId , version : version , metadata : metadata , db : metadata .db , initTx : tx }
45+ func NewFileStore (appId types.AppId , version int , metadata * Metadata , tx types.Transaction ) (* FileStore , error ) {
46+ var fileCache * FileCache
47+ var err error
48+ if metadata .dbType != system .DB_TYPE_SQLITE {
49+ fileCache , err = InitFileCache (metadata .Logger , metadata .config )
50+ if err != nil {
51+ return nil , fmt .Errorf ("error initializing file cache: %w" , err )
52+ }
53+ }
54+ return & FileStore {appId : appId , version : version , metadata : metadata , db : metadata .db , initTx : tx , fileCache : fileCache }, nil
4655}
4756
4857func (f * FileStore ) IncrementAppVersion (ctx context.Context , tx types.Transaction , metadata * types.AppMetadata ) error {
@@ -184,6 +193,17 @@ func (f *FileStore) GetFileBySha(sha string) ([]byte, string, error) {
184193}
185194
186195func (f * FileStore ) GetFileByShaTx (ctx context.Context , tx types.Transaction , sha string ) ([]byte , string , error ) {
196+ if f .fileCache != nil {
197+ content , compressionType , err := f .fileCache .GetCachedFile (ctx , sha )
198+ if err == nil {
199+ f .metadata .Trace ().Msgf ("Got file from cache: %s" , sha )
200+ return content , compressionType , nil
201+ }
202+ if err != nil && err != sql .ErrNoRows {
203+ return nil , "" , fmt .Errorf ("error getting cached file: %w" , err )
204+ }
205+ }
206+
187207 stmt , err := tx .PrepareContext (ctx , system .RebindQuery (f .metadata .dbType , "SELECT compression_type, content FROM files where sha = ?" ))
188208 if err != nil {
189209 return nil , "" , fmt .Errorf ("error preparing statement: %w" , err )
@@ -197,6 +217,14 @@ func (f *FileStore) GetFileByShaTx(ctx context.Context, tx types.Transaction, sh
197217 return nil , "" , fmt .Errorf ("error querying file table: %w" , err )
198218 }
199219
220+ if f .fileCache != nil {
221+ f .metadata .Trace ().Msgf ("Adding file to cache: %s" , sha )
222+ err = f .fileCache .AddCache (ctx , sha , compressionType , content )
223+ if err != nil {
224+ return nil , "" , fmt .Errorf ("error adding file to cache: %w" , err )
225+ }
226+ }
227+
200228 return content , compressionType , nil
201229}
202230
0 commit comments