Skip to content

Commit

Permalink
set ttl index
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Dec 3, 2024
1 parent a4ff5af commit 09f7d77
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/db/model/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func Setup(ctx context.Context, cfg *config.Config) error {
}
}

// Create TTL index for BTC price collection
if err := createTTLIndexes(ctx, database); err != nil {
log.Error().Err(err).Msg("Failed to create TTL index for BTC price")
return err
}

log.Info().Msg("Collections and Indexes created successfully.")
return nil
}
Expand Down Expand Up @@ -124,3 +130,16 @@ func createIndex(ctx context.Context, database *mongo.Database, collectionName s

log.Debug().Msg("Index created successfully on collection: " + collectionName)
}

func createTTLIndexes(ctx context.Context, database *mongo.Database) error {
collection := database.Collection(BtcPriceCollection)

// Create TTL index with expiration
index := mongo.IndexModel{
Keys: bson.D{{Key: "created_at", Value: 1}},
Options: options.Index().SetExpireAfterSeconds(3), // 5 minutes TTL
}

_, err := collection.Indexes().CreateOne(ctx, index)
return err
}

0 comments on commit 09f7d77

Please sign in to comment.