mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Storage buckets, indexer names, the bleve analyzer, the lock prefix, SSH host keys, the markdown front-matter key, the container data paths, the RPM repo id and the API security scheme all read as this product now, and app.example.ini matches the code again. The upstream maintainer list and the changelog generator config went with the release history they belonged to, and the citation comments state the fact rather than link the issue. Three inert rules turn out to have been inert because they named a module path this tree does not use: gci grouped no first-party imports under prefix(), and depguard let models/migrations import models and modules/structs. They now name github.com/hanzoai/git. The config example also documented .gitea/workflows while the code has read .hanzo/workflows for some time. The authorized_keys marker follows the delegate-hook precedent: the new name is written and the old one is still recognised, because a marker we do not know is copied through as a hand-added key and leaves a revoked key authorized. The stored user_setting key keeps its value; only a migration may change that. Co-authored-by: Hanzo Dev <[email protected]>
120 lines
2.8 KiB
Go
120 lines
2.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cache
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/hanzoai/git/modules/setting"
|
|
|
|
_ "github.com/hanzoai/git/modules/cache/chi/memcache" //nolint:depguard // memcache plugin for cache, it is required for config "ADAPTER=memcache"
|
|
)
|
|
|
|
var defaultCache StringCache
|
|
|
|
// Init start cache service
|
|
func Init() error {
|
|
if defaultCache == nil {
|
|
c, err := NewStringCache(setting.CacheService.Cache)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for range 10 {
|
|
if err = c.Ping(); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defaultCache = c
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const (
|
|
testCacheKey = "DefaultCache.TestKey"
|
|
// SlowCacheThreshold marks cache tests as slow
|
|
// set to 30ms per discussion: a cache miss must not be fatal
|
|
// TODO: Replace with metrics histogram
|
|
SlowCacheThreshold = 30 * time.Millisecond
|
|
)
|
|
|
|
// Test performs delete, put and get operations on a predefined key
|
|
// returns
|
|
func Test() (time.Duration, error) {
|
|
if defaultCache == nil {
|
|
return 0, errors.New("default cache not initialized")
|
|
}
|
|
|
|
testData := hex.EncodeToString(make([]byte, 500))
|
|
|
|
start := time.Now()
|
|
|
|
if err := defaultCache.Delete(testCacheKey); err != nil {
|
|
return 0, fmt.Errorf("expect cache to delete data based on key if exist but got: %w", err)
|
|
}
|
|
if err := defaultCache.Put(testCacheKey, testData, 10); err != nil {
|
|
return 0, fmt.Errorf("expect cache to store data but got: %w", err)
|
|
}
|
|
testVal, hit := defaultCache.Get(testCacheKey)
|
|
if !hit {
|
|
return 0, errors.New("expect cache hit but got none")
|
|
}
|
|
if testVal != testData {
|
|
return 0, errors.New("expect cache to return same value as stored but got other")
|
|
}
|
|
|
|
return time.Since(start), nil
|
|
}
|
|
|
|
// GetCache returns the currently configured cache
|
|
func GetCache() StringCache {
|
|
return defaultCache
|
|
}
|
|
|
|
// 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 defaultCache == nil || setting.CacheService.TTL == 0 {
|
|
return getFunc()
|
|
}
|
|
cached, exist := defaultCache.Get(key)
|
|
if !exist {
|
|
value, err := getFunc()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
return value, defaultCache.Put(key, value, setting.CacheService.TTLSeconds())
|
|
}
|
|
return cached, nil
|
|
}
|
|
|
|
// GetInt64 returns key value from cache with callback when no key exists in cache
|
|
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
|
|
s, err := GetString(key, func() (string, error) {
|
|
v, err := getFunc()
|
|
return strconv.FormatInt(v, 10), err
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if s == "" {
|
|
return 0, nil
|
|
}
|
|
return strconv.ParseInt(s, 10, 64)
|
|
}
|
|
|
|
// Remove key from cache
|
|
func Remove(key string) {
|
|
if defaultCache == nil {
|
|
return
|
|
}
|
|
_ = defaultCache.Delete(key)
|
|
}
|