cache

package
v0.0.0-...-c6d803e Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 19 Imported by: 6

README

about

####Note: This is an early release with significant changes. Please take a backup before using with any real data and report any issues you find.

This package is used to persist encrypted SN items to a local key-value store and then only sync deltas on subsequent calls.

usage

under construction

sign in

sio, _ := gosn.SignIn(gosn.SignInInput{
    Email:    "user@example.com",
    Password: "topsecret",
})

get cache session

cs, _ := cache.ImportSession(&sio.Session, "")

sync items to database

cso, _ := cache.Sync(cache.SyncInput{
    Session: cs,
})

retrieve items

var cItems []cache.Item
_ = cso.DB.All(&cItems)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanCacheItems

func CleanCacheItems(db *storm.DB, items Items, close bool) error

CleanCacheItems marks Cache Items as clean (Dirty = false) and resets dirtied date to the provided database.

func DeleteCacheItems

func DeleteCacheItems(db *storm.DB, items Items, close bool) error

DeleteCacheItems deletes Cache Items from the provided database.

func GenCacheDBPath

func GenCacheDBPath(session Session, dir, appName string) (string, error)

GenCacheDBPath generates a path to a database file to be used as a cache of encrypted items The filename is a SHA2 hash of a concatenation of the following in order to be both unique and avoid concurrent usage: - part of the session authentication key (so that caches are unique to a user) - the server URL (so that caches are server specific) - the requesting application name (so that caches are application specific).

func SaveCacheItems

func SaveCacheItems(db *storm.DB, items Items, close bool) error

SaveCacheItems saves Cache Items to the provided database.

func SaveEncryptedItems

func SaveEncryptedItems(db *storm.DB, items items.EncryptedItems, close bool) error

SaveEncryptedItems converts to cache items and persists to db.

func SaveItems

func SaveItems(s *Session, db *storm.DB, its items.Items, close bool) error

SaveItems saves Items to the provided database.

func SaveNotes

func SaveNotes(s *Session, db *storm.DB, notes items.Notes, close bool) error

SaveNotes encrypts, converts to cache items, and then persists to db.

func SaveTags

func SaveTags(db *storm.DB, s *Session, tags items.Tags, close bool) error

SaveTags encrypts, converts to cache items, and then persists to db.

Types

type CleanInput

type CleanInput struct {
	*Session
	Close                 bool
	UnreferencedItemsKeys bool
}

type Item

type Item struct {
	// String fields (16 bytes each) - ordered for optimal memory layout
	UUID        string `storm:"id,unique"`
	Content     string
	ContentType string `storm:"index"`
	ItemsKeyID  string
	EncItemKey  string
	CreatedAt   string
	UpdatedAt   string

	// Pointer field (8 bytes)
	DuplicateOf *string

	// time.Time is 24 bytes (3 words on 64-bit)
	DirtiedDate time.Time

	// Integer fields (8 bytes each)
	CreatedAtTimestamp int64
	UpdatedAtTimestamp int64

	// Boolean fields packed together (1 byte each, minimal padding)
	Deleted bool
	Dirty   bool
}

type Items

type Items []Item

func ToCacheItems

func ToCacheItems(items items.EncryptedItems, clean bool) (pitems Items)

func (Items) ToItems

func (pi Items) ToItems(s *Session) (items.Items, error)

func (Items) UUIDs

func (i Items) UUIDs() []string

func (Items) Validate

func (i Items) Validate() error

func (Items) ValidateSaved

func (i Items) ValidateSaved() error

type RateLimitBackoff

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

RateLimitBackoff tracks exponential backoff for rate limiting

type Session

type Session struct {
	*session.Session
	CacheDB     *storm.DB
	CacheDBPath string
}

func GetSession

func GetSession(httpClient *retryablehttp.Client, loadSession bool, sessionKey, server string, debug bool) (Session, string, error)

GetSession returns a cache session that encapsulates a gosn-v2 session with additional configuration for managing a local cache database.

func ImportSession

func ImportSession(gs *auth.SignInResponseDataSession, path string) (*Session, error)

ImportSession creates a new Session from an existing gosn.Session instance with the option of specifying a path for the db other than the home folder.

func (Session) Gosn

func (s Session) Gosn() session.Session

func (*Session) RemoveDB

func (s *Session) RemoveDB()

type SyncError

type SyncError struct {
	Type      SyncErrorType
	Original  error
	Message   string
	Retryable bool
	BackoffMs int64
}

SyncError provides detailed error information

func (*SyncError) Error

func (e *SyncError) Error() string

type SyncErrorType

type SyncErrorType int

SyncErrorType represents different types of sync errors

const (
	SyncErrorUnknown SyncErrorType = iota
	SyncErrorRateLimit
	SyncErrorValidation
	SyncErrorAuthentication
	SyncErrorItemsKey
	SyncErrorConflict
	SyncErrorNetwork
)

type SyncInput

type SyncInput struct {
	*Session
	Close bool
}

type SyncOutput

type SyncOutput struct {
	DB *storm.DB
}

func Sync

func Sync(si SyncInput) (so SyncOutput, err error)

Sync will push any dirty items to SN and make database cache consistent with SN.

func SyncWithRetry

func SyncWithRetry(si SyncInput, maxRetries int) (so SyncOutput, err error)

SyncWithRetry performs sync with intelligent retry logic

type SyncToken

type SyncToken struct {
	SyncToken string `storm:"id,unique"`
	CreatedAt time.Time
}

Jump to

Keyboard shortcuts

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