Introduce func NewCacheStore(opts Options) (CacheStore, error) and a method New(opts Options) CacheStore to the CacheStore interface
#56
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes are made ?
I introduced a new method
New(opts Options) CacheStoreto theCacheStoreinterface and implemented theNew() methodin everyCacheStoreavailable here.NewInMemoryStore,NewMemcachedStore,NewMemcachedBinaryStore,NewMemcachedBinaryStoreWithConfig,NewRedisCache&NewRedisCacheWithPoolfunctions.func NewCacheStore(opts Options) (CacheStore, error)which internally uses theNew(opts Options) CacheStoremethod.persistence.CacheStorein middlewarefunc Cache(store *persistence.CacheStore) gin.HandlerFunc& wrote a test for this, which seems to be working fine.example/example.goandREADME.mdfocusing 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 useInMemoryStore, they had to callNewInMemoryStorefunction. forRedisStorethey had to callNewRedisCache.Besides, by calling
NewRedisCache, we getRedisStorenotpersistence.CacheStore.If we wanted to get the
RedisStoreaspersistence.CacheStore, we had to do the following: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 inoptsstruct 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
CacheStoreto the middleware as the underlying every CacheStore we get fromNewCacheStore(opts Options) (CacheStore, error)is actually pointerAlso 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 thisRegisterfunction.Users who were using
NewInMemoryStore,NewMemcachedStore,NewMemcachedBinaryStore,NewMemcachedBinaryStoreWithConfig,NewRedisCacheorNewRedisCacheWithPoolfunctions, will need to replace those with the following:This will provide a
RedisStoretypeCacheStore