func NewRatelimit(limit int64, duration time.Duration, redisHost string,
	redisConnPoolSize int, redisPrefix string) *Ratelimit {
	return &Ratelimit{
		limit:    limit,
		duration: duration,
		rs:       redis.NewRedisStorage(redisHost, redisConnPoolSize, redisPrefix),
	}
}
Example #2
0
// Gets a new instance of LRCache, providing all the reqired params for Redis and LRU Cache
func NewLRCache(redisHost string, redisConnPoolSize int, redisPrefix string, size int) (*LRCache, error) {
	result := &LRCache{
		lruCache:   nil,
		redisCache: redis.NewRedisStorage(redisHost, redisConnPoolSize, redisPrefix),
	}
	// When evictning an item from the in-memoro LRU cache, save the item on redis as a second caching tier
	result.onEvict = func(key interface{}, val interface{}) {
		if str, ok := key.(string); ok {
			result.redisCache.Set(str, val)
		}
	}
	l, err := lru.NewWithEvict(size, result.onEvict)
	result.lruCache = l
	if err != nil {
		return nil, err
	}
	return result, nil
}