/// <summary> /// This method loads the data from cache into a Hashtable. /// /// Pass a SockIO object which is ready to receive data and a Hashtable /// to store the results. /// </summary> /// <param name="sock">socket waiting to pass back data</param> /// <param name="hm">hashmap to store data into</param> /// <param name="asString">if true, and if we are using NativehHandler, return string val</param> private void LoadItems(SockIO sock, Hashtable hm, bool asString) { while(true) { string line = sock.ReadLine(); if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems line").Replace("$$Line$$", line)); } if(line.StartsWith(VALUE)) { string[] info = line.Split(' '); string key = info[1]; int flag = int.Parse(info[2], new NumberFormatInfo()); int length = int.Parse(info[3], new NumberFormatInfo()); if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems header").Replace("$$Key$$", key).Replace("$$Flags$$", flag.ToString(new NumberFormatInfo())).Replace("$$Length$$", length.ToString(new NumberFormatInfo()))); } // read obj into buffer byte[] buf = new byte[length]; sock.Read(buf); sock.ClearEndOfLine(); // ready object object o; // check for compression if((flag & F_COMPRESSED) != 0) { try { // read the input stream, and write to a byte array output stream since // we have to read into a byte array, but we don't know how large it // will need to be, and we don't want to resize it a bunch GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf)); MemoryStream bos = new MemoryStream(buf.Length); int count; byte[] tmp = new byte[2048]; while((count = gzi.Read(tmp, 0, tmp.Length)) > 0) { bos.Write(tmp, 0, count); } // store uncompressed back to buffer buf = bos.ToArray(); gzi.Close(); } catch(IOException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } } // we can only take out serialized objects if((flag & F_SERIALIZED) == 0) { if(_primitiveAsString || asString) { // pulling out string value if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems retrieve as string")); } o = Encoding.GetEncoding(_defaultEncoding).GetString(buf); } else { // decoding object try { o = NativeHandler.Decode(buf); } catch(Exception e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } } } else { // deserialize if the data is serialized try { MemoryStream memStream = new MemoryStream(buf); o = new BinaryFormatter().Deserialize(memStream); if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name)); } } catch(SerializationException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } } // store the object into the cache hm[ key ] = o ; } else if(END == line) { if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems finished")); } break; } } }
private static void Test() { SerializableClass targetObj = new SerializableClass(); Type t = typeof(SerializableClass); string filePath = @"d:\tmp\"; BinaryFormatter binaryFormatter = new BinaryFormatter(); DataContractSerializer xmlSerializer = new DataContractSerializer(t); DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(t); Stopwatch sw = new Stopwatch(); // DataContractSerializer using (Stream stream = File.OpenWrite(filePath + xmlSerializer.GetType())) { sw.Start(); xmlSerializer.WriteObject(stream, targetObj); sw.Stop(); Console.WriteLine(xmlSerializer.GetType().Name + "\t\tシリアライズ\t" + sw.Elapsed); sw.Reset(); } using (Stream stream = File.OpenRead(filePath + xmlSerializer.GetType())) { sw.Start(); xmlSerializer.ReadObject(stream); sw.Stop(); Console.WriteLine(xmlSerializer.GetType().Name + "\t\tデシリアライズ\t" + sw.Elapsed); sw.Reset(); } // DataContractJsonSerializer using (Stream stream = File.OpenWrite(filePath + jsonSerializer.GetType())) { sw.Start(); jsonSerializer.WriteObject(stream, targetObj); sw.Stop(); Console.WriteLine(jsonSerializer.GetType().Name + "\tシリアライズ\t" + sw.Elapsed); sw.Reset(); } using (Stream stream = File.OpenRead(filePath + jsonSerializer.GetType())) { sw.Start(); jsonSerializer.ReadObject(stream); sw.Stop(); Console.WriteLine(jsonSerializer.GetType().Name + "\tデシリアライズ\t" + sw.Elapsed); sw.Reset(); } // BinaryFormatter using (Stream stream = File.OpenWrite(filePath + binaryFormatter.GetType())) { sw.Start(); binaryFormatter.Serialize(stream, targetObj); sw.Stop(); Console.WriteLine(binaryFormatter.GetType().Name + "\t\t\tシリアライズ\t" + sw.Elapsed); sw.Reset(); } using (Stream stream = File.OpenRead(filePath + binaryFormatter.GetType())) { sw.Start(); binaryFormatter.Deserialize(stream); sw.Stop(); Console.WriteLine(binaryFormatter.GetType().Name + "\t\t\tデシリアライズ\t" + sw.Elapsed); sw.Reset(); } }
/// <summary> /// This method loads the data from cache into a Hashtable. /// /// Pass a SockIO object which is ready to receive data and a Hashtable /// to store the results. /// </summary> /// <param name="sock">socket waiting to pass back data</param> /// <param name="hm">hashmap to store data into</param> /// <param name="asString">if true, and if we are using NativehHandler, return string val</param> private void LoadItems(string[] keys,Hashtable hm, bool asString) { string line = ""; int length = 0; if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems line").Replace("$$Line$$", line)); } List<GetOpResult> opResult = memcachedProvider.Get(keys); foreach(GetOpResult resultEntry in opResult) { string key = resultEntry.Key; int flag = (int)resultEntry.Flag; if(log.IsDebugEnabled) { log.Debug(GetLocalizedString("loaditems header").Replace("$$Key$$", key).Replace("$$Flags$$", flag.ToString(new NumberFormatInfo())).Replace("$$Length$$", length.ToString(new NumberFormatInfo()))); } // read obj into buffer byte[] buf =(byte[])resultEntry.Value; // ready object object o; // check for compression if((flag & F_COMPRESSED) != 0) { try { // read the input stream, and write to a byte array output stream since // we have to read into a byte array, but we don't know how large it // will need to be, and we don't want to resize it a bunch GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf)); MemoryStream bos = new MemoryStream(buf.Length); int count; byte[] tmp = new byte[2048]; while((count = gzi.Read(tmp, 0, tmp.Length)) > 0) { bos.Write(tmp, 0, count); } // store uncompressed back to buffer buf = bos.ToArray(); gzi.Close(); } catch(IOException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems uncompression IOException").Replace("$$Key$$", key), e); } } // we can only take out serialized objects if((flag & F_SERIALIZED) == 0) { if(_primitiveAsString || asString) { // pulling out string value if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems retrieve as string")); } o = Encoding.GetEncoding(_defaultEncoding).GetString(buf); } else { // decoding object try { o = NativeHandler.Decode(buf); } catch(Exception e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems deserialize error").Replace("$$Key$$", key), e); } } } else { // deserialize if the data is serialized try { MemoryStream memStream = new MemoryStream(buf); o = new BinaryFormatter().Deserialize(memStream); if(log.IsInfoEnabled) { log.Info(GetLocalizedString("loaditems deserializing").Replace("$$Class$$", o.GetType().Name)); } } catch(SerializationException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } throw new IOException(GetLocalizedString("loaditems SerializationException").Replace("$$Key$$", key), e); } } // store the object into the cache hm[ key ] = o ; } }
/// <summary> This method loads the data from cache into a Map. /// /// Pass a SockIO object which is ready to receive data and a HashMap<br/> /// to store the results. /// /// </summary> /// <param name="sock">socket waiting to pass back data /// </param> /// <param name="hm">hashmap to store data into /// </param> /// <throws> IOException if io exception happens while reading from socket </throws> private void LoadItems(SockIOPool.SockIO sock, IDictionary hm) { while (true) { string line = sock.ReadLine(); if(log.IsDebugEnabled) log.Debug("line: " + line); if (line.StartsWith(VALUE)) { string[] info = line.Split(' '); string key = info[1]; int flag = int.Parse(info[2]); int length = int.Parse(info[3]); if(log.IsDebugEnabled) log.Debug("key: " + key + "\n" + "flags: " + flag + "\n" + "length: " + length); // read obj into buffer byte[] buf = new byte[length]; sock.Read(buf); sock.ClearEOL(); // ready object object o; // check for compression if ((flag & F_COMPRESSED) != 0) { try { // read the input stream, and write to a byte array output stream since // we have to read into a byte array, but we don't know how large it // will need to be, and we don't want to resize it a bunch GZipInputStream gzi = new GZipInputStream(new MemoryStream(buf)); MemoryStream bos = new MemoryStream(buf.Length); int count; byte[] tmp = new byte[2048]; while ((count = gzi.Read(tmp, 0, tmp.Length)) != - 1) { bos.Write(tmp, 0, count); } // store uncompressed back to buffer buf = bos.ToArray(); gzi.Close(); } catch(IOException ioe) { if(log.IsErrorEnabled) log.Error("IOException thrown while trying to uncompress input stream for key: " + key, ioe); throw new IOException("IOException thrown while trying to uncompress input stream for key: " + key); } } // we can only take out serialized objects if ((flag & F_SERIALIZED) == 0) { if(log.IsInfoEnabled) log.Info("this object is not a serialized object. Stuffing into a string."); o = new string(UTF8Encoding.UTF8.GetChars(buf)); } else { // deserialize if the data is serialized BinaryReader ois = new BinaryReader(new MemoryStream(buf)); try { o = new BinaryFormatter().Deserialize(ois.BaseStream); if(log.IsInfoEnabled) log.Info("deserializing " + o.GetType().FullName); } catch(Exception e) { if(log.IsErrorEnabled) log.Error("ClassNotFoundException thrown while trying to deserialize for key: " + key, e); throw new IOException("failed while trying to deserialize for key: " + key); } } // store the object into the cache hm.Add(key, o); } else if (END.Equals(line)) { if(log.IsDebugEnabled) log.Debug("finished reading from cache server"); break; } } }