<commit_before>// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cache

import (
	"fmt"
	"strconv"

	"code.gitea.io/gitea/modules/setting"

	mc "gitea.com/go-chi/cache"

	_ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
)

var (
	conn mc.Cache
)

func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
	return mc.NewCacher(mc.Options{
		Adapter:       cacheConfig.Adapter,
		AdapterConfig: cacheConfig.Conn,
		Interval:      cacheConfig.Interval,
	})
}

// NewContext start cache service
func NewContext() error {
	var err error

	if conn == nil && setting.CacheService.Enabled {
		if conn, err = newCache(setting.CacheService.Cache); err != nil {
			return err
		}
	}

	return err
}

// GetCache returns the currently configured cache
func GetCache() mc.Cache {
	return conn
}

// GetString returns the key value from cache with callback when no key exists in cache
func GetString(key string, getFunc func() (string, error)) (string, error) {
	if conn == nil || setting.CacheService.TTL == 0 {
		return getFunc()
	}
	if !conn.IsExist(key) {
		var (
			value string
			err   error
		)
		if value, err = getFunc(); err != nil {
			return value, err
		}
		err = conn.Put(key, value, setting.CacheService.TTLSeconds())<commit_msg>Test cache during init (#17852)<commit_after>// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cache

import (
	"fmt"
	"strconv"

	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/setting"

	mc "gitea.com/go-chi/cache"

	_ "gitea.com/go-chi/cache/memcache" // memcache plugin for cache
)

var (
	conn mc.Cache
)

func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
	return mc.NewCacher(mc.Options{
		Adapter:       cacheConfig.Adapter,
		AdapterConfig: cacheConfig.Conn,
		Interval:      cacheConfig.Interval,
	})
}

// NewContext start cache service
func NewContext() error {
	var err error

	if conn == nil && setting.CacheService.Enabled {
		if conn, err = newCache(setting.CacheService.Cache); err != nil {
			return err
		}
		const testKey = "__gitea_cache_test"
		const testVal = "test-value"
		if err = conn.Put(testKey, testVal, 10); err != nil {
			return err
		}
		val := conn.Get(testKey)
		if valStr, ok := val.(string); !ok || valStr != testVal {
			// If the cache is full, the Get may not read the expected value stored by Put.
			// Since we have checked that Put can success, so we just show a warning here, do not return an error to panic.
			log.Warn("cache (adapter:%s, config:%s) doesn't seem to work correctly, set test value '%v' but get '%v'",
				setting.CacheService.Cache.Adapter, setting.CacheService.Cache.Conn,
				testVal, val,
			)
		}
	}

	return err
}

// GetCache returns the currently configured cache
func GetCache() mc.Cache {
	return conn
}

// GetString returns the key value from cache with callback when no key exists in cache
func GetString(key string, getFunc func() (string, error)) (string, error) {
	if conn == nil || setting.CacheService.TTL == 0 {
		return getFunc()
	}
	if !conn.IsExist(key) {
		var (
			value string
			err   error
		)
		if value, err = getFunc(); err != nil {
			return value, err
		}
		err = conn.Put(key, value, setting.CacheService.TTLSeconds())
{'fraction_non_alphanumeric': 0.1145104895104895, 'fraction_numerical': 0.004953379953379953, 'mean_word_length': 3.4125964010282774, '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': '5794403', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1126, 'n_words': 371}