/// <summary> /// delete cache /// </summary> /// <param name="commandInfo"></param> /// <param name="parameters"></param> /// <param name="configItem"></param> private void DeleteCache(CommandInfo commandInfo, ServerConfigItem configItem, object[] parameters) { object[] deleteDatas = null; try { foreach (var cacheDelete in configItem.CacheDeletes) { var deleteCommandConfig = ServerConfig.GetItem(cacheDelete.Server, cacheDelete.Command); if (deleteCommandConfig == null) { this.logManager.Exception(new CsException("Not Find Cache Config {0}.{1} By {2}.{3} Cache Delete" , cacheDelete.Server, cacheDelete.Command , commandInfo.Server, commandInfo.Name )); continue; } else if (null != deleteCommandConfig.CacheVersion) { var version = ServerCache.GetVersion(); //update version ServerCache.Cache.Set(deleteCommandConfig.CacheVersion, version, ServerConfig.GetVersionExpires(commandInfo.Server, deleteCommandConfig.CacheVersion)); if (ServerCache.LogWriter.Enable) { ServerCache.LogWriter.WriteTimeLine("Delete Cache Version: " + deleteCommandConfig.CacheVersion); } } else { //delete deleteDatas = this.GetDeleteParametersDatas(cacheDelete, parameters); var cacheKey = ServerCache.BuildKey(deleteCommandConfig.CacheKey, deleteDatas); ServerCache.Cache.Delete(cacheKey); if (ServerCache.LogWriter.Enable) { ServerCache.LogWriter.WriteTimeLine("Delete Cache: " + cacheKey); } } } } catch (CsException) { throw; } catch (Exception e) { this.logManager.Exception(e); } }
/// <summary> /// 移除一个命令缓存 /// </summary> /// <param name="server"></param> /// <param name="command"></param> /// <param name="parameters"></param> /// <exception cref="ArgumentException">not find command config</exception> /// <returns>remove cache name</returns> public static string RemoveCache(string server, string command, params object[] parameters) { var deleteCommandConfig = ServerConfig.GetItem(server, command); if (deleteCommandConfig == null) { throw new ArgumentException(string.Format("not find {0}.{1} command config", server, command)); } //delete var cacheKey = ServerCache.BuildKey(deleteCommandConfig.CacheKey, parameters); ServerCache.Cache.Delete(cacheKey); if (logWriter.Enable) { logWriter.WriteTimeLine("Delete Cache: " + cacheKey); } return(cacheKey); }
/// <summary> /// delete cache /// </summary> /// <param name="commandInfo"></param> /// <param name="parameters"></param> /// <param name="configItem"></param> private void DeleteCache(CommandInfo commandInfo, ServerConfigItem configItem, object[] parameters) { object[] deleteDatas = null; try { foreach (var cacheDelete in configItem.CacheDeletes) { var deleteCommandConfig = ServerConfig.GetItem(cacheDelete.Server, cacheDelete.Command); if (deleteCommandConfig == null) { this.serverLogger.CommandCacheDelete(commandInfo.Server, commandInfo.Name, cacheDelete.Server, cacheDelete.Command, "Not Find"); continue; } else if (null != deleteCommandConfig.CacheVersion) { var version = ServerCache.GetVersion(); //update version ServerCache.Cache.Set(deleteCommandConfig.CacheVersion, version, ServerConfig.GetVersionExpires(commandInfo.Server, deleteCommandConfig.CacheVersion)); } else { //delete deleteDatas = this.GetDeleteParametersDatas(cacheDelete, parameters); var cacheKey = ServerCache.BuildKey(deleteCommandConfig.CacheKey, deleteDatas); ServerCache.Cache.Delete(cacheKey); } } } catch (CsException) { throw; } catch (Exception e) { this.serverLogger.CommandCacheException(commandInfo.Server, commandInfo.Name, e); } }
/// <summary> /// invoke /// </summary> /// <param name="commandInfo"></param> /// <param name="parameters"></param> /// <param name="valueBuffer"></param> /// <param name="cacheType"></param> /// <returns></returns> private CsResult Invoke(CommandInfo commandInfo, object[] parameters, out byte[] valueBuffer, out ServerCacheType cacheType) { /* * this method ,try catch, safe cache error */ //variable init string cacheKey = null; string cacheVersion = null; object invokeValue = null; var result = CsResult.Fail; //bool hasContent = false; // ServerConfigItem configItem = null; bool isCache = false; if (ServerCache.Enable) { configItem = ServerConfig.GetItem(commandInfo.Server, commandInfo.Name); isCache = configItem != null && configItem.CacheExpires > 0; } //init out valueBuffer = null; cacheType = ServerCacheType.NoCache; //cache get if (isCache) { //mode = version if (configItem.CacheVersion != null) { cacheVersion = ServerCache.Cache.Get(configItem.CacheVersion); } //get cache cacheKey = ServerCache.BuildKey(configItem.CacheKey, cacheVersion, parameters); try { valueBuffer = ServerCache.Cache.Get <byte[]>(cacheKey); result = CsResult.Success; } catch (Exception e) { this.serverLogger.CommandCacheException(commandInfo.Server, commandInfo.Name, e); } cacheType = valueBuffer != null && result == CsResult.Success ? ServerCacheType.Cached : ServerCacheType.NoCache; } //invoke if (valueBuffer == null) { cacheType = ServerCacheType.NoCache; result = CsResult.Success; //no ObjectCache if (invokeValue == null) { invokeValue = commandInfo.Method.Invoke(commandInfo.Instance, parameters); } //是否有内容 //hasContent = invokeValue != null; //buffer = bodylength+body, parameter index + parameter length + parameter using (var m = new MemoryStream(CsSocket.BODY_BUFFER_SIZE)) { //body using (var m2 = new MemoryStream(CsSocket.BODY_BUFFER_SIZE)) { if (invokeValue == null) { //length m.Write(BitConverter.GetBytes((int)-1), 0, CsSocket.LENGTH_BUFFER_SIZE); } else { CsSerializer.Serialize(m2, invokeValue); //length m.Write(BitConverter.GetBytes((int)m2.Length), 0, CsSocket.LENGTH_BUFFER_SIZE); //body m2.WriteTo(m); } } //parameters // count m.Write(BitConverter.GetBytes((int)commandInfo.OutsIndexLength), 0, CsSocket.LENGTH_BUFFER_SIZE); using (var m2 = new MemoryStream(CsSocket.BODY_BUFFER_SIZE)) { for (int i = 0, index = 0; i < commandInfo.OutsIndexLength; i++) { m2.SetLength(0); m2.Position = 0; // index = (int)commandInfo.OutsIndex[i]; //index m.Write(BitConverter.GetBytes(index), 0, CsSocket.LENGTH_BUFFER_SIZE); // if (parameters[index] == null) { //length m.Write(BitConverter.GetBytes((int)-1), 0, CsSocket.LENGTH_BUFFER_SIZE); } else { CsSerializer.Serialize(m2, parameters[index]); //length m.Write(BitConverter.GetBytes((int)m2.Length), 0, CsSocket.LENGTH_BUFFER_SIZE); //body m2.WriteTo(m); } } } // valueBuffer = m.ToArray(); } } //cache set //if (result == CsResult.Success && cacheType == CacheType.NoCache && hasContent && commandInfo.CacheExpires > 0) if (isCache && result == CsResult.Success && cacheType == ServerCacheType.NoCache) { try { //set cache ServerCache.Cache.Set(cacheKey, valueBuffer, configItem.CacheExpires); cacheType = ServerCacheType.NewCache; } catch (Exception e) { this.serverLogger.CommandCacheException(commandInfo.Server, commandInfo.Name, e); } } //delete cache if (ServerCache.Enable && isCache && configItem.CacheDeletes != null) { this.DeleteCache(commandInfo, configItem, parameters); } return(result); }
private void NewCommandInvoke(Socket socket, byte parameterLength, string serverName, string commandName, int commandStart) { object value = null; object[] parameters = null; var commandInfo = ServerCommand.GetCommand(serverName, commandName); if (commandInfo == null) { value = string.Format("Not Find Command:{0}.{1}", serverName, commandName); } else if (commandInfo.Parameters.Length != parameterLength) { value = string.Format("{0}.{1} Client Command Parameters And Server Parameters No Match", serverName, commandName); } else { //get parameter if (parameterLength > 0) { parameters = new object[parameterLength]; for (var i = 0; i < parameterLength; i++) { if (commandInfo.Parameters[i].IsOut) { CsSocket.Receive(socket); } else { parameters[i] = CsSocket.Receive(socket, commandInfo.Parameters[i].ParameterType); } } } } //cache byte[] cacheBuffer = null; String cacheVersion = null, cacheKey = null; var cacheType = ServerCacheType.NoCache; ServerConfigItem configItem = null; bool cacheEnable = false; //no error & get cache if (value == null) { if (ServerCache.Enable) { configItem = ServerConfig.GetItem(commandInfo.Server, commandInfo.Name); cacheEnable = configItem != null && configItem.CacheExpires > 0; } //cache get if (cacheEnable) { //mode = version if (configItem.CacheVersion != null) { try { cacheVersion = ServerCache.Cache.Get(configItem.CacheVersion); if (cacheVersion != null && cacheVersion != "") { //get cache cacheKey = ServerCache.BuildKey(configItem.CacheKey, cacheVersion, parameters); //value = ServerCache.Cache.Get(cacheKey, commandInfo.Method.ReturnType); cacheBuffer = ServerCache.Cache.Get <byte[]>(cacheKey); cacheType = cacheBuffer != null ? ServerCacheType.Cached : ServerCacheType.NoCache; } else { //no version , set no cache cacheEnable = false; } } catch (Exception e) { this.logManager.Exception(e); } } else { //get cache cacheKey = ServerCache.BuildKey(configItem.CacheKey, null, parameters); try { //value = ServerCache.Cache.Get(cacheKey, commandInfo.Method.ReturnType); cacheBuffer = ServerCache.Cache.Get <byte[]>(cacheKey); cacheType = cacheBuffer != null ? ServerCacheType.Cached : ServerCacheType.NoCache; } catch (Exception e) { this.logManager.Exception(e); } } } } var result = false; //cached if (cacheType == ServerCacheType.Cached) { socket.Send(cacheBuffer); result = true; } //no error ,invoke else if (value == null) { //var invokeStart = DateTime.Now; try { value = commandInfo.Method.Invoke(commandInfo.Instance, parameters); result = true; } catch (TargetInvocationException exception) { result = false; this.logManager.Exception(exception.GetBaseException()); value = exception.GetBaseException().Message; } catch (Exception exception) { result = false; this.logManager.Exception(exception); value = exception.Message; } //invokeSeconds = DateTime.Now.Subtract(invokeStart).TotalSeconds; //build value Buffer if (result) { using (var m = new MemoryStream()) { //write result m.WriteByte((byte)CsResult.Success); //send out parameter length //buffer parameter length , parameter index + parameter length + parameter body + bodylength + body //parameter count writer m.WriteByte(commandInfo.OutsIndexLength); //parameter write if (commandInfo.OutsIndexLength > 0) { for (byte i = 0, l = commandInfo.OutsIndexLength; i < l; i++) { //write index m.WriteByte(commandInfo.OutsIndex[i]); //write parameter CsSocket.WriteDataItem(m, parameters[commandInfo.OutsIndex[i]]); } } //body CsSocket.WriteDataItem(m, value); //build value buffer completed //svae value buffer to cache //if (result == CsResult.Success && cacheType == CacheType.NoCache && hasContent && commandInfo.CacheExpires > 0) if (cacheEnable && cacheType == ServerCacheType.NoCache) { cacheBuffer = m.ToArray(); try { //set cache ServerCache.Cache.Set(cacheKey, cacheBuffer, configItem.CacheExpires); cacheType = ServerCacheType.NewCache; } catch (Exception e) { this.logManager.Exception(e); } } //delete cache if (ServerCache.Enable && cacheEnable && configItem.CacheDeletes != null) { this.DeleteCache(commandInfo, configItem, parameters); } //send value if (cacheType == ServerCacheType.NewCache) { socket.Send(cacheBuffer); } else { socket.Send(m.GetBuffer(), 0, (int)m.Length, SocketFlags.None); } } } } else { //parameter error result = false; //value = "parameter error"; } //send error if (result == false) { using (var m = new MemoryStream()) { m.WriteByte((byte)CsResult.Fail); //write failure message CsSocket.WriteDataItem(m, (string)value); // socket.Send(m.GetBuffer(), 0, (int)m.Length, SocketFlags.None); } } // if (this.logMessage.Enable) { //this.logMessage.WriteTimeLine("{0}: {1}.{2}:{3}, Cache:{4}, InvokeSeconds:{5},Seconds:{6}", // this.id, serverName, commandName, result ? "Success" : "Failure", cacheType, invokeSeconds, DateTime.Now.Subtract(start).TotalSeconds); //stopwatch.Stop(); //var elapsedMilliseconds = stopwatch.ElapsedMilliseconds; var elapsedMilliseconds = Environment.TickCount - commandStart; //this.logMessage.WriteTimeLine("{0}: {1}.{2}:{3}, Cache:{4}, Elapsed:{5}", // this.id, serverName, commandName, result ? "Success" : "Failure", cacheType, stopwatch.Elapsed.TotalSeconds); this.logMessage.WriteTimeLine("{0}\t{1}\t{2}.{3}\t{4}\t{5}", this.id , cacheType , serverName , commandName , elapsedMilliseconds , (result ? "OK" : (string)value)); } }