Exemplo n.º 1
0
        /// <summary>
        /// 取得數據
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>數據</returns>
        public ValueType Get(KeyType key)
        {
            CacheRequestTimes += 1;
            if (CacheData.ContainsKey(key))
            {
                CacheRequestHitTimes += 1;
                CacheDataInfo <KeyType, ValueType> ret = CacheData[key];
                if (ret.IsNeedToWrite)
                {
                    DBWriter.Add(ref ret);
                }
                return(ret.Value);
            }

            CacheDataInfo <KeyType, ValueType> wData = DBWriter.Find(key);

            if (wData != null)
            {
                Add(key, wData.Value);
                return(wData.Value);
            }

            ValueType data = DBHandler.LoadData(key);

            if (data != null)
            {
                Add(key, data);
            }
            return(data);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 刪除一筆數據
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="data"></param>
 public void Delete(KeyType key, ValueType data)
 {
     CacheData.TryRemove(key, out CacheDataInfo <KeyType, ValueType> info);
     if (info == null)
     {
         info = new CacheDataInfo <KeyType, ValueType>(key, data);
     }
     DBWriter.AddDelete(ref info);
 }
Exemplo n.º 3
0
        /// <summary>
        /// 建立數據
        /// </summary>
        /// <param name="data">數據</param>
        /// <returns>ID</returns>
        public KeyType Create(ValueType data)
        {
            KeyType ID   = GetNewIdentity();
            var     info = new CacheDataInfo <KeyType, ValueType>(ID, data);

            DBWriter.AddCreate(ref info);
            Add(ID, data);
            return(ID);
        }
Exemplo n.º 4
0
        internal CacheDataInfo <KeyType, ValueType> Find(KeyType key)
        {
            CacheDataInfo <KeyType, ValueType> ret = null;

            if (IndexData.ContainsKey(key))
            {
                ret = IndexData[key];
            }
            return(ret);
        }
Exemplo n.º 5
0
 public void Add(ref CacheDataInfo <KeyType, ValueType> info, bool IsRetry = false)
 {
     IndexData[info.Key] = info;
     CacheData.Enqueue(info);
     info.WriteWaitTime = DateTime.Now;
     if (!IsRetry)
     {
         RequestTimes += 1;
     }
     waiter.Set();
 }
Exemplo n.º 6
0
        /// <summary>
        /// 新增或更新數據
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public void Add(KeyType key, ValueType data)
        {
            if (CacheData.ContainsKey(key))
            {
                CacheData[key].Value = data;
                return;
            }
            if (IsFull())
            {
                RemoveEldest();
            }

            CacheData[key] = new CacheDataInfo <KeyType, ValueType>(key, data);
        }
Exemplo n.º 7
0
 private void DBHandler_SaveResultCallback(IEnumerable <KeyType> success = null, IEnumerable <KeyType> fails = null)
 {
     if (success != null)
     {
         foreach (var key in success)
         {
             if (IndexData.ContainsKey(key))
             {
                 var cData = IndexData[key];
                 cData.IsNeedToWrite  = false;
                 cData.SaveRetryTimes = 0;
             }
             Remove(key);
         }
     }
     if (fails != null)
     {
         foreach (var key in fails)
         {
             CacheDataInfo <KeyType, ValueType> cData = null;
             if (IndexData.ContainsKey(key))
             {
                 cData = IndexData[key];
             }
             else
             {
                 cData = CacheData.AsParallel().Where(o => o.Key.Equals(key)).FirstOrDefault();
             }
             if (cData != null)
             {
                 TotalRetryTimes      += 1;
                 cData.SaveRetryTimes += 1;
                 if (cData.SaveRetryTimes < Cache <KeyType, ValueType> .MaxSaveRetryTimes)
                 {
                     Add(ref cData, true);
                 }
                 else
                 {
                     OnDiscardData?.Invoke(cData);
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// 儲存數據
 /// </summary>
 /// <param name="info">數據</param>
 protected void Save(ref CacheDataInfo <KeyType, ValueType> info)
 {
     Save(info.Key, info.Value);
 }
Exemplo n.º 9
0
 protected void DBWriter_OnDiscardData(CacheDataInfo <KeyType, ValueType> data)
 {
     Remove(data.Key, false);
 }
Exemplo n.º 10
0
 public void AddDelete(ref CacheDataInfo <KeyType, ValueType> info)
 {
     info.Action = CacheDataInfo <KeyType, ValueType> .ActionType.Delete;
     Add(ref info);
 }
Exemplo n.º 11
0
        public void Add(KeyType key, ValueType data)
        {
            var dt = new CacheDataInfo <KeyType, ValueType>(key, data);

            Add(ref dt);
        }