Skip to content

Conversation

@masudur-rahman
Copy link

What changes are made ?

I introduced a new method New(opts Options) CacheStore to the CacheStore interface and implemented the New() method in every CacheStore available here.

  • Removed NewInMemoryStore, NewMemcachedStore, NewMemcachedBinaryStore, NewMemcachedBinaryStoreWithConfig, NewRedisCache & NewRedisCacheWithPool functions.
  • Introduced func NewCacheStore(opts Options) (CacheStore, error) which internally uses the New(opts Options) CacheStore method.
  • Removed pointer from persistence.CacheStore in middleware func Cache(store *persistence.CacheStore) gin.HandlerFunc & wrote a test for this, which seems to be working fine.
  • Updated every tests where the current changes make impact.
  • Updated example/example.go and README.md focusing on how to use the current changes.

Why the changes are made ?

func NewCacheStore(opts Options) (CacheStore, error)

There was no general way of getting a CacheStore. If they wanted to use InMemoryStore, they had to call NewInMemoryStore function. for RedisStore they had to call NewRedisCache.

Besides, by calling NewRedisCache, we get RedisStore not persistence.CacheStore.
If we wanted to get the RedisStore as persistence.CacheStore, we had to do the following:

var store persistence.CacheStore
store = persistence.NewRedisStore(...)

I wanted to make it more general. So, I implemented a function NewCacheStore(opts Options) (CacheStore, error). Now users can call this function no matter whichever cache store they need, they just need to define configs in opts struct according to the expected cache store.

func Cache(store persistence.CacheStore) gin.HandlerFunc

I think we don't need to use pointer for an interface. We can just pass the CacheStore to the middleware as the underlying every CacheStore we get from NewCacheStore(opts Options) (CacheStore, error) is actually pointer

Also I completed the func TestCache(t *testing.T).

Where and how do these changes affect ?

I added func Register(name string, adapter CacheStore), so in future if anyone wants to add a new/custom cache store they will need to register their cache store using this Register function.

Users who were using NewInMemoryStore, NewMemcachedStore, NewMemcachedBinaryStore, NewMemcachedBinaryStoreWithConfig, NewRedisCache or NewRedisCacheWithPool functions, will need to replace those with the following:

opts := Options{
	Adapter: AdapterRedisStore,
	AdapterConfig: AdapterConfig{
		RedisConfig: &RedisConfig{...},
	},
	DefaultExpiration: defaultExpiration,
}
store, err := persistence.NewCacheStore(opts)

This will provide a RedisStore type CacheStore

Signed-off-by: Masudur Rahman <masudjuly02@gmail.com>
Signed-off-by: Masudur Rahman <masudjuly02@gmail.com>
Signed-off-by: Masudur Rahman <masudjuly02@gmail.com>
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

Successfully merging this pull request may close these issues.

1 participant