using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;
using System.Xml.Linq;


namespace molilian.core
{
    public partial class TkConfigCore
    {
        private static readonly object _lockObj = new();
        private static TkConfigDTO _cached;


        public static TkConfigDTO Get(bool force = false)
        {
            if (!force && _cached != null) return _cached;

            string cache_key = $"cache:tk_config";

            var item = CenterHub.Redis.Get<TkConfigDTO>(cache_key);
            if (force || item == null)
            {
                lock (_lockObj)
                {
                    using var conn = CenterHub.GetOpenConnection();
                    item = new DBContext.Table(conn, "tk_config").Get<TkConfigDTO>("id>0", new { });
                    if (item == null) return default;
                    CenterHub.Redis.Set(cache_key, item, 30 * 86400);
                }
            }
            _cached = item;
            return item;
        }
        public static void Refresh()
        {
            _ = Get(true);
        }

    }

}
