hrsystem

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 7 Imported by: 0

README

hrsystem: oncecache example

hrsystem contains a oncecache example. It is a simple staff directory backed by a pretend DB. It uses oncecache to reduce DB calls.

One of the key things that hrsystem demonstrates is cache entry propagation across a composite cache.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Department

type Department struct {
	Name  string      `json:"name"`
	Staff []*Employee `json:"staff"`
}

func (*Department) LogValue

func (d *Department) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Department) String

func (d *Department) String() string

type Employee

type Employee struct {
	Name string `json:"name"`
	Role string `json:"role"`
	ID   int    `json:"id"`
}

func (*Employee) LogValue

func (e *Employee) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Employee) String

func (e *Employee) String() string

type HRCache

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

HRCache is a caching layer for any HRSystem implementation. HRCache is a composite cache, using a oncecache.Cache instance for each of the Org, Department, and Employee entity types. Using the oncecache event mechanism, a cache entry fill of one cache is propagated to other caches. For example, a call to HRCache.GetOrg populates not only that single cache entry, but propagates values to the Department cache, which in turn propagates values to the Employee cache.

func NewHRCache

func NewHRCache(log *slog.Logger, db HRSystem) *HRCache

NewHRCache wraps db with a caching layer.

func (*HRCache) Close

func (c *HRCache) Close() error

Close clears the cache.

func (*HRCache) GetDepartment

func (c *HRCache) GetDepartment(ctx context.Context, deptName string) (*Department, error)

GetDepartment implements HRSystem.

func (*HRCache) GetEmployee

func (c *HRCache) GetEmployee(ctx context.Context, id int) (*Employee, error)

GetEmployee implements HRSystem.

func (*HRCache) GetOrg

func (c *HRCache) GetOrg(ctx context.Context, orgName string) (*Org, error)

GetOrg implements HRSystem.

func (*HRCache) Stats

func (c *HRCache) Stats() *Stats

Stats returns the cache stats.

type HRDatabase

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

HRDatabase pretends to be a database, but it's really just an in-memory structure loaded from a JSON file. It implements HRSystem.

func NewHRDatabase

func NewHRDatabase(log *slog.Logger, datafile string) (*HRDatabase, error)

NewHRDatabase returns a new HRDatabase instance loaded from json datafile.

func (*HRDatabase) GetDepartment

func (db *HRDatabase) GetDepartment(ctx context.Context, dept string) (*Department, error)

GetDepartment implements HRSystem.

func (*HRDatabase) GetEmployee

func (db *HRDatabase) GetEmployee(ctx context.Context, id int) (*Employee, error)

GetEmployee implements HRSystem.

func (*HRDatabase) GetOrg

func (db *HRDatabase) GetOrg(_ context.Context, org string) (*Org, error)

GetOrg implements HRSystem.

func (*HRDatabase) Stats

func (db *HRDatabase) Stats() *Stats

Stats returns database invocation stats.

type HRSystem

type HRSystem interface {
	GetOrg(ctx context.Context, org string) (*Org, error)
	GetDepartment(ctx context.Context, dept string) (*Department, error)
	GetEmployee(ctx context.Context, ID int) (*Employee, error)
}

type Org

type Org struct {
	Name        string        `json:"name"`
	Departments []*Department `json:"departments"`
}

func (*Org) LogValue

func (o *Org) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Org) String

func (o *Org) String() string

type Stats

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

Stats tracks how many times a method has been invoked.

func NewStats

func NewStats() *Stats

NewStats returns a new Stats instance.

func (*Stats) GetDepartment

func (s *Stats) GetDepartment() int

GetDepartment returns the number of times GetDepartment has been called.

func (*Stats) GetEmployee

func (s *Stats) GetEmployee() int

GetEmployee returns the number of times GetEmployee has been called.

func (*Stats) GetOrg

func (s *Stats) GetOrg() int

GetOrg returns the number of times GetOrg has been called.

func (*Stats) LogValue

func (s *Stats) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Stats) String

func (s *Stats) String() string

Jump to

Keyboard shortcuts

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