<commit_before>package vfs

import (
	"encoding/hex"
	"encoding/json"
	"sync"
	"time"

	"github.com/cozy/cozy-stack/pkg/config"
	"github.com/cozy/cozy-stack/pkg/crypto"
	"github.com/go-redis/redis"
)

// A DownloadStore is essentially an object to store Archives & Files by keys
type DownloadStore interface {
	AddFile(domain, filePath string) (string, error)
	AddArchive(domain string, archive *Archive) (string, error)
	GetFile(domain, key string) (string, error)
	GetArchive(domain, key string) (*Archive, error)
}

// downloadStoreTTL is the time an Archive stay alive
const downloadStoreTTL = 1 * time.Hour

// downloadStoreCleanInterval is the time interval between each download
// cleanup.
const downloadStoreCleanInterval = 1 * time.Hour

var globalStoreMu sync.Mutex
var globalMemStore *memStore
var globalRedisStore *redisStore

type memRef struct {
	val interface{}
	exp time.Time
}

func storeCleaner() {
	for range time.Tick(downloadStoreCleanInterval) {
		globalMemStore.clean()
	}
}

// GetStore returns the DownloadStore.
func GetStore() DownloadStore {
	globalStoreMu.Lock()
	defer globalStoreMu.Unlock()
	if globalRedisStore != nil {
		return globalRedisStore
	}
	if globalMemStore != nil {
		return globalMemStore
	}
	opts := config.CacheOptions()
	if opts == nil {
		globalMemStore = &memStore{vals: make(map[string]*memRef)}
		go storeCleaner()
		return globalMemStore
	}
	redisClient := redis.NewClient(opts)
	return &redisStore{redisClient}
}

type memStore struct {
	mu   sync.Mutex<commit_msg>Use one global variable<commit_after>package vfs

import (
	"encoding/hex"
	"encoding/json"
	"sync"
	"time"

	"github.com/cozy/cozy-stack/pkg/config"
	"github.com/cozy/cozy-stack/pkg/crypto"
	"github.com/go-redis/redis"
)

// A DownloadStore is essentially an object to store Archives & Files by keys
type DownloadStore interface {
	AddFile(domain, filePath string) (string, error)
	AddArchive(domain string, archive *Archive) (string, error)
	GetFile(domain, key string) (string, error)
	GetArchive(domain, key string) (*Archive, error)
}

// downloadStoreTTL is the time an Archive stay alive
const downloadStoreTTL = 1 * time.Hour

// downloadStoreCleanInterval is the time interval between each download
// cleanup.
const downloadStoreCleanInterval = 1 * time.Hour

var globalStoreMu sync.Mutex
var globalStore DownloadStore

type memRef struct {
	val interface{}
	exp time.Time
}

func storeCleaner() {
	for range time.Tick(downloadStoreCleanInterval) {
		globalStore.(*memStore).clean()
	}
}

// GetStore returns the DownloadStore.
func GetStore() DownloadStore {
	globalStoreMu.Lock()
	defer globalStoreMu.Unlock()
	if globalStore != nil {
		return globalStore
	}
	opts := config.CacheOptions()
	if opts == nil {
		globalStore = &memStore{vals: make(map[string]*memRef)}
		go storeCleaner()
	} else {
		globalStore = &redisStore{redis.NewClient(opts)}
	}
	return globalStore
}

type memStore struct {
	mu   sync.Mutex
{'fraction_non_alphanumeric': 0.1036357458375807, 'fraction_numerical': 0.0013591573224600749, 'mean_word_length': 4.761252446183953, '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': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1972143', 'n_tokens_mistral': 975, 'n_tokens_neox': 951, 'n_words': 261}