<commit_before>// Package filecache provides simple file system cache
package filecache

import (
	"crypto/md5"
	"encoding/hex"
	"io/ioutil"
	"log"
	"os"
	"time"
)

// Type Handler holds the cache directory and cache
// life in minutes
type Handler struct {
	Dir  string
	Life float64
}

// Set saves content to the cache
func (h Handler) Set(key string, data []byte) error {
	return ioutil.WriteFile(h.filename(key), data, 0644)
}

// Get returns content from the cache
func (h Handler) Get(key string) []byte {
	file := h.filename(key)
	result, err := ioutil.ReadFile(file)
	if err != nil {
		return nil
	}
	fi, err := os.Stat(file)
	panicon(err)
	age := time.Since(fi.ModTime()).Minutes()
	if age > h.Life {
		err := os.Remove(file)
		panicon(err)
		return nil
	}
	return result
}

// filename returns the full path to the cache file
func (h Handler) filename(key string) string {
	return h.dir() + "/" + hash(key)
}

// dir creates and returns a valid cache directory
// temp dir is returned if Handler.Dir is not set
func (h Handler) dir() string {
	if h.Dir == "" {
		h.Dir = os.TempDir()
		return h.Dir
	}

	yes, err := exists(h.Dir)
	panicon(err)
	if !yes {
		err := os.MkdirAll(h.Dir, 0666)
		panicon(err)
	}
	return h.Dir<commit_msg>Filename function is now exported<commit_after>// Package filecache provides simple file system cache
package filecache

import (
	"crypto/md5"
	"encoding/hex"
	"io/ioutil"
	"log"
	"os"
	"time"
)

// Type Handler holds the cache directory and cache
// life in minutes
type Handler struct {
	Dir  string
	Life float64
}

// Set saves content to the cache
func (h Handler) Set(key string, data []byte) error {
	return ioutil.WriteFile(h.Filename(key), data, 0644)
}

// Get returns content from the cache
func (h Handler) Get(key string) []byte {
	file := h.Filename(key)
	result, err := ioutil.ReadFile(file)
	if err != nil {
		return nil
	}
	fi, err := os.Stat(file)
	panicon(err)
	age := time.Since(fi.ModTime()).Minutes()
	if age > h.Life {
		err := os.Remove(file)
		panicon(err)
		return nil
	}
	return result
}

// Filename returns the full path to the cache file
func (h Handler) Filename(key string) string {
	return h.dir() + "/" + hash(key)
}

// dir creates and returns a valid cache directory
// temp dir is returned if Handler.Dir is not set
func (h Handler) dir() string {
	if h.Dir == "" {
		h.Dir = os.TempDir()
		return h.Dir
	}

	yes, err := exists(h.Dir)
	panicon(err)
	if !yes {
		err := os.MkdirAll(h.Dir, 0666)
		panicon(err)
	}
	return h.Dir
{'fraction_non_alphanumeric': 0.13088587390263368, 'fraction_numerical': 0.008778930566640064, 'mean_word_length': 3.4371681415929203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6146105', 'n_tokens_mistral': 950, 'n_tokens_neox': 902, 'n_words': 301}