Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MongoDB as a cache implementation #661

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

DavidGregory084
Copy link

This PR adds support for MongoDB as a cache implementation.

The cache is implemented as a MongoDB collection. The user must specify the database name and collection name to use for their cache.

Cache entries are created in the following format:

{
  "_id": <key>, // This is Mongo's unique key field which is indexed by default
  "value": <value>,
  "expiresAt": <current time + TTL value if provided, else null>
}

MongoDB uses BSON as its serialization format. In order to capture this a BsonCodec type was added to this library along with BsonEncoder and BsonDecoder.

An integration module scalacache-mongo-circe has been provided which provides BsonCodec given an io.circe.Codec. This is done by converting io.circe.Json into org.bson.BsonValue and vice versa.

Document expiry is done using MongoDB's support for TTL indexes.

A TTL index is created for the expiresAt field with the expireAfterSeconds property set to 0. This indicates to Mongo that the document should be expired at the timestamp stored in the field.

When cache entries are inserted, the TTL value provided is added to the current time to establish the expiry time for that entry. If no TTL was provided, the field is null and the document does not expire.

Contributed by @rabinarai1 and myself on behalf of the @opencastsoftware open source team. 👋

BTW, we have noticed that this project is in between releases. I can see that #336 is tracking the progress on that. We'd be happy to help if there is anything we can pick up?

@DavidGregory084 DavidGregory084 marked this pull request as ready for review March 8, 2022 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants