/// <summary> /// 设置域名转换IP地址 /// </summary> /// <param name="key">域名</param> /// <param name="ipAddress">IP地址</param> private static void setDomainIp(HashBytes key, ref DomainIPAddress ipAddress) { Monitor.Enter(domainIpLock); try { domainIps.Set(ref key, ipAddress); if (domainIps.Count == DomainIPAddressConfig.Default.CacheCount) { domainIps.UnsafePopValue(); } } finally { Monitor.Exit(domainIpLock); } }
/// <summary> /// 设置文件缓存 /// </summary> /// <param name="path"></param> /// <param name="fileCache"></param> /// <param name="fileSize"></param> private void set(ref FileCacheKey path, FileCache fileCache, int fileSize) { Monitor.Enter(fileLock); try { fileCache.Size = fileSize; FileCache oldFileCache = files.Set(ref path, fileCache); freeCacheSize -= fileSize; if (oldFileCache == null) { while (freeCacheSize < 0 && files.Count > 1) { freeCacheSize += files.UnsafePopValue().Size; } } else if (oldFileCache != fileCache) { freeCacheSize += oldFileCache.Size; } } finally { Monitor.Exit(fileLock); } }
/// <summary> /// 设置缓存数据 /// </summary> /// <param name="index"></param> /// <param name="data"></param> internal void SetCache(long index, byte[] data) { if (DataCache == null) { IndexCache.UnsafeAdd(ref index, new HashBytes { SubArray = new SubArray <byte> { Array = data } }); if ((freeCacheSize -= data.Length) < 0) { HashBytes removeData; do { removeData = IndexCache.UnsafePopValue(); }while ((freeCacheSize += removeData.SubArray.Array.Length) < 0); } } else { HashBytes hashData = data; Dictionary <HashBytes, DataCache> cache = DataCache[hashData.HashCode & 0xff]; if (cache == null) { IndexCache.UnsafeAdd(ref index, hashData); freeCacheSize -= data.Length; DataCache[hashData.HashCode & 0xff] = cache = DictionaryCreator.CreateHashBytes <DataCache>(); cache.Add(hashData, new DataCache { Data = hashData, Index = index, Count = 1 }); } else { DataCache count; if (cache.TryGetValue(hashData, out count)) { IndexCache.UnsafeAdd(ref index, count.Data); ++count.Count; freeCacheSize -= data.Length; cache[hashData] = count; } else { IndexCache.UnsafeAdd(ref index, hashData); freeCacheSize -= data.Length; cache.Add(hashData, new DataCache { Data = hashData, Index = index, Count = 1 }); } } DataCache removeData; while (freeCacheSize < 0) { removeData.Data = IndexCache.UnsafePopValue(); freeCacheSize += removeData.Data.SubArray.Array.Length; cache = DataCache[removeData.Data.HashCode & 0xff]; if (cache != null && cache.TryGetValue(removeData.Data, out removeData)) { if (--removeData.Count == 0) { cache.Remove(removeData.Data); } else { cache[removeData.Data] = removeData; } } } } }
/// <summary> /// 日志流数据 /// </summary> /// <param name="data"></param> private void onLog(AutoCSer.Net.TcpServer.ReturnValue <Log <valueType, modelType> .Data> data) { if (isError == 0) { if (data.Type == Net.TcpServer.ReturnType.Success) { try { valueType value = data.Value.Value.Value; RandomKey <keyType> key = getKey(data.Value.Value.Value); KeyValue <valueType, EventWaitHandle> queueValue; switch (data.Value.Type) { case LogType.Insert: Monitor.Enter(logLock); if (queue.TryGetOnly(key, out queueValue)) { queue.SetOnly(key, new KeyValue <valueType, EventWaitHandle>(value, null)); Monitor.Exit(logLock); } else { try { queue.Set(ref key, new KeyValue <valueType, EventWaitHandle>(value, null)); if (queue.Count > maxCount) { queue.UnsafePopValue(); } } finally { Monitor.Exit(logLock); } } return; case LogType.Update: Monitor.Enter(logLock); if (queue.TryGetOnly(key, out queueValue)) { if (queueValue.Value == null) { try { AutoCSer.MemberCopy.Copyer <modelType> .Copy(queueValue.Key, value, data.Value.Value.MemberMap); } finally { Monitor.Exit(logLock); } if (data.Value.Value.MemberMap == null && !isErrorMemberMap) { isErrorMemberMap = true; log.add(AutoCSer.Log.LogType.Warn, "客户端缓存数据缺少成员位图信息 " + typeof(valueType).fullName()); } return; } queue.Remove(ref key, out queueValue); } Monitor.Exit(logLock); MemberMapValueLinkPool <valueType> .PushNotNull(value); return; case LogType.Delete: MemberMapValueLinkPool <valueType> .PushNotNull(value); Monitor.Enter(logLock); queue.Remove(ref key, out queueValue); Monitor.Exit(logLock); return; } } catch (Exception error) { log.add(AutoCSer.Log.LogType.Error, error); } } this.error(); } }