Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here is some thoughts ~ #51

Open
yeqown opened this issue Jun 24, 2020 · 0 comments
Open

Here is some thoughts ~ #51

yeqown opened this issue Jun 24, 2020 · 0 comments

Comments

@yeqown
Copy link
Contributor

yeqown commented Jun 24, 2020

for now, all cache implement CacheInterface, but some cache is using CacheInterface:
could I understand this as the Decorator pattern ?

// LoadableCache represents a cache that uses a function to load data
type LoadableCache struct {
	loadFunc   loadFunction
	cache      CacheInterface
	setChannel chan *loadableKeyValue
}

// Marshaler is the struct that marshal and unmarshal cache values
type Marshaler struct {
	cache cache.CacheInterface
}

AND StoreInterface is same to CacheInterface,

// CacheInterface represents the interface for all caches (aggregates, metric, memory, redis, ...)
type CacheInterface interface {
	Get(key interface{}) (interface{}, error)
	Set(key, object interface{}, options *store.Options) error
	Delete(key interface{}) error
	Invalidate(options store.InvalidateOptions) error
	Clear() error
	GetType() string
}

// StoreInterface is the interface for all available stores
type StoreInterface interface {
	Get(key interface{}) (interface{}, error)
	Set(key interface{}, value interface{}, options *Options) error
	Delete(key interface{}) error
	Invalidate(options InvalidateOptions) error
	Clear() error
	GetType() string
}

according to the above case, there is my thoughts:

  1. all cache implement CacheInterface, all store implement StoreInterface, this is good, but the two interface should be different (CacheInterface is an wrapper of StoreInterface, but provide extern functions like: "metrics, loadable, chain and others"), I would change CacheInterface as:
// CacheInterface represents the interface for all caches (aggregates, metric, memory, redis, ...)
type CacheInterface interface {
	Get(key interface{}) (interface{}, error)
	Set(key, object interface{}, options *store.Options) error
	Delete(key interface{}) error
	Invalidate(options store.InvalidateOptions) error
	Clear() error

        GetType() string                                     // return cache type
        GetCodec()  codec.CodecInterface{}  // get codec instance
        GetStore() store.StoreInterface{}        // get current store instance
}
  1. all cache constructor should return CacheInterface to limit usage.

  2. I also think about extern functions, Could we coding them as WithLoadFunc() WithMetrics() WithChain()

type Option struct {
    marshal mashaler.MarshalerInterface
    // ...  other fields
}

func WithMarshal(params ...string) *Option {
  // do something
}

func NewCache(store store.StoreInterface, opts ...*Options) CacheInterface {
      // to construct a Cache
}

I hope this image can help to understand me ~~~

image

@yeqown yeqown changed the title I want know why not this arch ? Here is some thoughts ~ Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant