dbpool

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 7 Imported by: 0

README

ngr-dbpool in-memory db-connection-cache implementation (based on sqlx db connections inside)

import

import "github.com/NGRsoftlab/ngr-dbpool"

example

// create db conn cache with default 10sec storing and 5sec garbage collecting interval
LocalDBCache := New(10*time.Second, 5*time.Second)
defer LocalCache.ClearAll()

connTimeOut := 5*time.Second
execTimeOut := 55*time.Second

Ctx, cancel := context.WithTimeout(ctx, execTimeOut)
defer cancel()

db, err := dbpool.GetConnectionByParams(Ctx, LocalDBCache,
	connTimeOut,
	"postgres", "<postgres connection string>")
if err != nil {
	return err
}

err = db.SelectContext(Ctx, result, db.Rebind("<sql query to get some information>"), args...)
if err != nil {
	return err
}
// ... some other logic

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConnectionByParams

func GetConnectionByParams(Ctx context.Context, connCache *SafeDbMapCache,
	duration time.Duration, driver, connString string) (*sqlx.DB, error)

GetConnectionByParams - get *sqlx.DB from cache (if exists, with default-json-mapperFunc) or create new and put into cache

func GetConnectionWithMapper added in v1.1.0

func GetConnectionWithMapper(
	Ctx context.Context,
	connCache *SafeDbMapCache,
	duration time.Duration,
	driver, connString string,
	mapperFunc *reflectx.Mapper) (*sqlx.DB, error)

GetConnectionWithMapper - get *sqlx.DB from cache (if exists, with set mapperFunc) or create new and put into cache

func JsonMapperFunc added in v1.1.0

func JsonMapperFunc() *reflectx.Mapper

Types

type PoolItem

type PoolItem struct {
	Expiration int64
	Duration   time.Duration
	Created    time.Time

	DB *sqlx.DB
}

type SafeDbMapCache

type SafeDbMapCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(defaultExpiration, cleanupInterval time.Duration) *SafeDbMapCache

New - initializing a new SafeDbMapCache cache

func (*SafeDbMapCache) ClearAll

func (c *SafeDbMapCache) ClearAll()

ClearAll - remove all items.

func (*SafeDbMapCache) Delete

func (c *SafeDbMapCache) Delete(key string) error

Delete - delete *sqlx.DB value by key. Return false if key not found

func (*SafeDbMapCache) ExpiredKeys

func (c *SafeDbMapCache) ExpiredKeys() (keys []string)

ExpiredKeys - returns list of expired keys.

func (*SafeDbMapCache) GC

func (c *SafeDbMapCache) GC()

GC - Garbage Collection cycle

func (*SafeDbMapCache) Get

func (c *SafeDbMapCache) Get(key string) (*sqlx.DB, bool)

Get - getting *sqlx.DB value by key

func (*SafeDbMapCache) GetItems

func (c *SafeDbMapCache) GetItems() (items []string)

GetItems - returns item list.

func (*SafeDbMapCache) Set

func (c *SafeDbMapCache) Set(key string, value *sqlx.DB, duration time.Duration)

Set - setting *sqlx.DB value by key

func (*SafeDbMapCache) StartGC

func (c *SafeDbMapCache) StartGC()

StartGC - start Garbage Collection

Jump to

Keyboard shortcuts

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