github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"path/filepath"
     7  
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/cache/locations"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    10  )
    11  
    12  type StoreLocation uint
    13  
    14  const (
    15  	FsCache StoreLocation = iota
    16  	MemoryCache
    17  )
    18  
    19  // NoCache indicates that we are not caching or reading from the cache
    20  var NoCache *Store = nil
    21  
    22  // Storer stores items in the given location. Cache is agnostic
    23  // of the actual location of the items (files in FS terms) and
    24  // Storer is responsible for all low-level work.
    25  type Storer interface {
    26  	// Writer returns io.WriteCloser for the given cache item
    27  	Writer(path string) (io.WriteCloser, error)
    28  	// Reader returns io.ReaderCloser for the given cache item
    29  	Reader(path string) (io.ReadCloser, error)
    30  	Remove(path string) error
    31  	Stat(path string) (*locations.ItemInfo, error)
    32  }
    33  
    34  // Locator is a struct implementing the Locator interface. It can describe its
    35  // location in the cache
    36  type Locator interface {
    37  	CacheLocations() (string, string, string)
    38  }
    39  
    40  // Unmarshaler is a struct implementing Unmarshaler can be read from binary by
    41  // calling UnmarshalCache
    42  type Unmarshaler interface {
    43  	UnmarshalCache(vers uint64, reader io.Reader) error
    44  }
    45  
    46  // Marshaler is a struct implementing the Marshaler interface. It can be
    47  // written to binary by calling MarshalCache
    48  type Marshaler interface {
    49  	MarshalCache(writer io.Writer) error
    50  }
    51  
    52  // StoreOptions used by Store
    53  type StoreOptions struct {
    54  	Location StoreLocation
    55  	Chain    string
    56  
    57  	// Optional
    58  
    59  	RootDir string
    60  	// If ReadOnly is true, then we will not write to the cache
    61  	ReadOnly bool
    62  }
    63  
    64  func (s *StoreOptions) location() (loc Storer, err error) {
    65  	if s == nil {
    66  		log.Fatal("should not happen ==> implementation error in location.")
    67  		return
    68  	}
    69  	switch s.Location {
    70  	case MemoryCache:
    71  		loc, err = locations.Memory()
    72  	case FsCache:
    73  		fallthrough
    74  	default:
    75  		loc, err = locations.FileSystem()
    76  	}
    77  
    78  	return
    79  }
    80  
    81  func (s *StoreOptions) rootDir() (dir string) {
    82  	if s != nil && s.Location == MemoryCache {
    83  		return "memory"
    84  	}
    85  
    86  	if s == nil {
    87  		log.Fatal("should not happen ==> implementation error in location.")
    88  	} else if s.RootDir == "" {
    89  		dir = config.PathToCache(s.Chain)
    90  	}
    91  
    92  	if dir != "" {
    93  		// TODO: v1 suffix
    94  		return filepath.Join(dir, "v1")
    95  	}
    96  
    97  	return s.RootDir
    98  }