package district

import (
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
	"github.com/dgraph-io/ristretto"
)

var nameCache *ristretto.Cache

func init() {
	nameCache, _ = ristretto.NewCache(&ristretto.Config{
		NumCounters:        1024,
		MaxCost:            10 * 1024 * 1024,
		BufferItems:        64,
		IgnoreInternalCost: true,
	})
}

func getCachedDistrictItem(code string) (*svc.DistrictCacheItem, bool) {
	v, ok := nameCache.Get(code)
	if ok {
		return v.(*svc.DistrictCacheItem), true
	}
	return nil, false
}

func cacheDistrictItem(code string, nameList []string) {
	nameCache.Set(code, &svc.DistrictCacheItem{
		Code:     code,
		NameList: nameList,
	}, 1)
	nameCache.Wait()
}
