<commit_before>package cachemanager

import (
	"gopkg.in/redis.v3"
	"time"
)

// NewHandlerRedis creates a redis handler
func NewHandlerRedis(redisConfig *redis.Options, prefix string, ttl time.Duration) Handler {
	return &handlerRedis{
		client: redis.NewClient(redisConfig),
		handler: handler{
			prefix: prefix,
			ttl:    ttl,
		},
	}
}

func NewHandlerRedisFromConfiguration(conf *ConfigurationRedis) Handler {
	return NewHandlerRedis(&redis.Options{
		Addr:         conf.Addr,
	}, conf.Prefix, time.Duration(conf.TTL))
}

type handlerRedis struct {
	client *redis.Client
	handler
}

func (h handlerRedis) Del(key string) error {
	return h.client.Del(h.buildKey(key)).Err()
}

func (h handlerRedis) Get(key string) (interface{}, error) {
	// Initialize
	var o interface{}

	// Get item
	i, e := h.client.Get(h.buildKey(key)).Result()
	if e != nil && e == redis.Nil {
		return o, ErrCacheMiss
	}

	return []byte(i), e
}

func (h handlerRedis) Set(key string, value interface{}, ttl time.Duration) error {
	return h.client.Set(h.buildKey(key), value, ttl).Err()
}

func (h handlerRedis) Increment(key string, delta uint64) (uint64, error) {
	res, err := h.client.IncrBy(h.buildKey(key), int64(delta)).Result()
	if err == redis.Nil {
		return 0, ErrCacheMiss
	} else if err != nil {
		return 0, err
	}

	return uint64(res), err
}

func (h handlerRedis) Decrement(key string, delta uint64) (uint64, error) {
	res, err := h.client.DecrBy(h.buildKey(key), int64(delta)).Result()
	if err == redis.Nil {
		return 0, ErrCacheMiss
	} else if err != nil {
		return 0, err
	}

	return uint64(res), err
}

func (h handlerRedis) SetOnEvicted(f func(k string, v interface{})) Handler {
	panic("not yet implemented")
}

func (h handlerRedis) Test() error {
	panic("not yet implemented")
}<commit_msg>Added test to redis<commit_after>package cachemanager

import (
	"gopkg.in/redis.v3"
	"time"
)

// NewHandlerRedis creates a redis handler
func NewHandlerRedis(redisConfig *redis.Options, prefix string, ttl time.Duration) Handler {
	return &handlerRedis{
		client: redis.NewClient(redisConfig),
		handler: handler{
			prefix: prefix,
			ttl:    ttl,
		},
	}
}

func NewHandlerRedisFromConfiguration(conf *ConfigurationRedis) Handler {
	return NewHandlerRedis(&redis.Options{
		Addr:         conf.Addr,
	}, conf.Prefix, time.Duration(conf.TTL))
}

type handlerRedis struct {
	client *redis.Client
	handler
}

func (h handlerRedis) Del(key string) error {
	return h.client.Del(h.buildKey(key)).Err()
}

func (h handlerRedis) Get(key string) (interface{}, error) {
	// Initialize
	var o interface{}

	// Get item
	i, e := h.client.Get(h.buildKey(key)).Result()
	if e != nil && e == redis.Nil {
		return o, ErrCacheMiss
	}

	return []byte(i), e
}

func (h handlerRedis) Set(key string, value interface{}, ttl time.Duration) error {
	return h.client.Set(h.buildKey(key), value, ttl).Err()
}

func (h handlerRedis) Increment(key string, delta uint64) (uint64, error) {
	res, err := h.client.IncrBy(h.buildKey(key), int64(delta)).Result()
	if err == redis.Nil {
		return 0, ErrCacheMiss
	} else if err != nil {
		return 0, err
	}

	return uint64(res), err
}

func (h handlerRedis) Decrement(key string, delta uint64) (uint64, error) {
	res, err := h.client.DecrBy(h.buildKey(key), int64(delta)).Result()
	if err == redis.Nil {
		return 0, ErrCacheMiss
	} else if err != nil {
		return 0, err
	}

	return uint64(res), err
}

func (h handlerRedis) SetOnEvicted(f func(k string, v interface{})) Handler {
	panic("not yet implemented")
}

func (h handlerRedis) Test() error {
	// Initialize
	var e error
	k := "test"

	// Set
	e = h.Set(k, "1", 1)
	if e != nil {
		return e
	}

	// Return
	return h.Del(k)
}
{'fraction_non_alphanumeric': 0.14297341291372762, 'fraction_numerical': 0.011937059142702116, 'mean_word_length': 4.078512396694215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6513168', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1347, 'n_words': 361}