mongodbcacheadapters

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

GitHub go.mod Go version go.dev reference Go Report Card GitHub Twitter Follow

Cache Adapter implementation for MongoDB

A CacheAdapter implementation that allows to connect and use a MongoDB database instance.

Usage

Please refer to the following example for the correct usage:

package main

import (
	"log"
	"time"
	
	mongodbcacheadapters "github.com/tryvium-travels/golang-cache-adapters/mongodb"
)

func main() {
	exampleTTL := time.Hour

	adapter, err := mongodbcacheadapters.New(exampleTTL)
	if err != nil {
		// remember to check for errors
		log.Fatalf("Adapter initialization error: %s", err)
	}

	type exampleStruct struct {
		Value string
	}

	exampleKey := "a:mongodb:key"

	var exampleValue exampleStruct
	err = adapter.Get(exampleKey, &exampleValue)
	if err != nil {
		// remember to check for errors
		log.Fatalf("adapter.Get error: %s", err)
	}

	exampleKey = "another:mongodb:key"

	// nil TTL represents the default value put in the New function
	err = adapter.Set(exampleKey, exampleValue, nil)
	if err != nil {
		// remember to check for errors
		log.Fatalf("adapter.Get error: %s", err)
	}
}

Documentation

Overview

Package mongodbcacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for a MongoDB instance based cache, along with some helper methods to create instances.

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrDBConnectionNotCreated will come out if you try to create a database helper
	// over a database connection that is not yet created.
	ErrDBConnectionNotCreated = fmt.Errorf("cannot create a database helper instance over a never-created database connection")

	//ErrNilClient will come out if you try to create a CacheAdapter instance
	// when providing a nil MongoDB Client instance
	ErrNilClient = fmt.Errorf("cannot create the adapter with nil MongoDB Client instance")

	//ErrInvalidDatabaseName will come out if you try to create a CacheAdapter instance
	// when providing an invalid MongoDB Database name
	ErrInvalidDatabaseName = fmt.Errorf("cannot create the adapter with invalid MongoDB Database name")

	//ErrInvalidCollectionName will come out if you try to create a CacheAdapter instance
	// when providing an invalid MongoDB Collection name
	ErrInvalidCollectionName = fmt.Errorf("cannot create the adapter with invalid MongoDB Collection name")

	//ErrNilDatabase will come out if you try to create a CacheAdapter instance
	// when providing a nil MongoDB Database instance
	ErrNilDatabase = fmt.Errorf("cannot create the adapter with nil MongoDB Database instance")

	//ErrNilCollection will come out if you try to create a CacheAdapter instance
	// when providing a nil MongoDB Collection instance
	ErrNilCollection = fmt.Errorf("cannot create the adapter with nil MongoDB Collection instance")

	//ErrNilSession will come out if you try to create a CacheSessionAdapter instance
	// when providing a nil MongoDB Session instance
	ErrNilSession = fmt.Errorf("cannot create the session adapter with nil MongoDB Session instance")

	//ErrSessionClosed will come out if you try to do operation on an already
	// closed session
	ErrSessionClosed = fmt.Errorf("cannot use a closed connection")
)

Functions

func New

func New(client MongoClient, databaseName string, collectionName string, defaultTTL time.Duration) (cacheadapters.CacheAdapter, error)

NesSession create a new MongoDB Cache adapter from an existing MongoDB client and the name of the database and the collection, with a given default TTL.

func NewSession

func NewSession(collection MongoCollection, defaultTTL time.Duration) (cacheadapters.CacheSessionAdapter, error)

NesSession create a new MongoDB Session adapter

Types

type MongoClient

type MongoClient interface {
	Connect(ctx context.Context) error
	Disconnect(ctx context.Context) error
	Ping(ctx context.Context, rp *readpref.ReadPref) error
	StartSession(opts ...*options.SessionOptions) (mongo.Session, error)
	Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
}

type MongoCollection

type MongoCollection interface {
	FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
	InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
}

type MongoDBAdapter

type MongoDBAdapter struct {
	// contains filtered or unexported fields
}

func (*MongoDBAdapter) Delete

func (ma *MongoDBAdapter) Delete(key string) error

Delete deletes a key from the cache.

func (*MongoDBAdapter) Get

func (ma *MongoDBAdapter) Get(key string, objectRef interface{}) error

Get obtains a value from the cache using a key, then tries to unmarshal it into the object reference passed as parameter.

func (*MongoDBAdapter) OpenSession

func (*MongoDBAdapter) Set

func (ma *MongoDBAdapter) Set(key string, object interface{}, TTL *time.Duration) error

Set sets a value represented by the object parameter into the cache, with the specified key.

func (*MongoDBAdapter) SetTTL

func (ma *MongoDBAdapter) SetTTL(key string, newTTL time.Duration) error

SetTTL marks the specified key new expiration, deletes it via using cacheadapters.TTLExpired or negative duration.

type MongoDBSessionAdapter

type MongoDBSessionAdapter struct {
	// contains filtered or unexported fields
}

func (*MongoDBSessionAdapter) Close

func (msa *MongoDBSessionAdapter) Close() error

Close closes the Cache Session.

func (*MongoDBSessionAdapter) Delete

func (msa *MongoDBSessionAdapter) Delete(key string) error

Delete deletes a key from the cache.

func (*MongoDBSessionAdapter) Get

func (msa *MongoDBSessionAdapter) Get(key string, objectRef interface{}) error

func (*MongoDBSessionAdapter) Set

func (msa *MongoDBSessionAdapter) Set(key string, object interface{}, TTL *time.Duration) error

Set sets a value represented by the object parameter into the cache, with the specified key.

func (*MongoDBSessionAdapter) SetTTL

func (msa *MongoDBSessionAdapter) SetTTL(key string, newTTL time.Duration) error

SetTTL marks the specified key new expiration, deletes it via using cacheadapters.TTLExpired or negative duration.

type MongoDatabase

type MongoDatabase interface {
	Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
}

type MongoSession

type MongoSession interface {
	mongo.Session
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL