<commit_before>package redis_full

import (
	"errors"
	"github.com/garyburd/redigo/redis"
	"time"
)

// Wraps the Redis client to meet the Cache interface.
type RedisCache struct {
	pool              *redis.Pool
	defaultExpiration time.Duration
}

var ErrCacheMiss = errors.New("redis_full: key not found.")

// until redigo supports sharding/clustering, only one host will be in hostList
func NewRedisCache(host, password string, MaxIdle, MaxActive int, IdleTimeout, defaultExpiration time.Duration) RedisCache {
	var pool = &redis.Pool{
		MaxIdle:     MaxIdle,
		MaxActive:   MaxActive,
		IdleTimeout: IdleTimeout,
		Dial: func() (redis.Conn, error) {
			protocol := "tcp"
			c, err := redis.Dial(protocol, host)
			if err != nil {
				return nil, err
			}
			if len(password) > 0 {
				if _, err := c.Do("AUTH", password); err != nil {
					c.Close()
					return nil, err
				}
			} else {
				// check with PING
				if _, err := c.Do("PING"); err != nil {
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
		// custom connection test method
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			if _, err := c.Do("PING"); err != nil {
				return err
			}
			return nil
		},
	}
	return RedisCache{pool, defaultExpiration}
}

func (c RedisCache) CheckRedis() error {
	conn := c.pool.Get()
	defer conn.Close()

	if _, err := conn.Do("PING"); err != nil {
		return err
	} else {
		return nil
	}
}

func (c RedisCache) Life(key string) (int64, error) {
	conn := c.pool.Get()
	defer conn.Close()

	ok, err := c.Exists(key)
	if err != nil {
		return -1, err
	}
	if !ok {
		return -1, ErrCacheMiss<commit_msg>new redis method<commit_after>package redis_full

import (
	"errors"
	"github.com/garyburd/redigo/redis"
	"time"
)

// Wraps the Redis client to meet the Cache interface.
type RedisCache struct {
	pool              *redis.Pool
	defaultExpiration time.Duration
}

var ErrCacheMiss = errors.New("redis_full: key not found.")

// until redigo supports sharding/clustering, only one host will be in hostList
func NewRedisCache(host, password string, database, MaxIdle, MaxActive int, IdleTimeout, defaultExpiration time.Duration) RedisCache {
	var pool = &redis.Pool{
		MaxIdle:     MaxIdle,
		MaxActive:   MaxActive,
		IdleTimeout: IdleTimeout,
		Dial: func() (redis.Conn, error) {
			protocol := "tcp"
			c, err := redis.Dial(
				protocol,
				host,
				redis.DialDatabase(database),
				redis.DialPassword(password),
				redis.DialConnectTimeout(time.Second*5),
				redis.DialWriteTimeout(time.Second*3),
				redis.DialReadTimeout(time.Second*3),
			)
			if err != nil {
				return nil, err
			}
			return c, err
		},
	}
	return RedisCache{pool, defaultExpiration}
}

func (c RedisCache) CheckRedis() error {
	conn := c.pool.Get()
	defer conn.Close()

	if _, err := conn.Do("PING"); err != nil {
		return err
	} else {
		return nil
	}
}

func (c RedisCache) Life(key string) (int64, error) {
	conn := c.pool.Get()
	defer conn.Close()

	ok, err := c.Exists(key)
	if err != nil {
		return -1, err
	}
	if !ok {
		return -1, ErrCacheMiss
{'fraction_non_alphanumeric': 0.1286740692357936, 'fraction_numerical': 0.003919007184846506, 'mean_word_length': 3.1504065040650406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4563478', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1100, 'n_words': 303}