Ejemplo n.º 1
0
        internal Script DeserializeScript(string directRealPath, out bool isCacheValid)
        {
            LoadCachePool();

            Script sc = null;

            isCacheValid = true;

            if (!_cachePool.ContainsKey(directRealPath))
            {
                return(null);
            }

            FileInfo f = new FileInfo(directRealPath);

            CacheModel.ScriptCache scCache = _cachePool[directRealPath];
            if (scCache != null &&
                scCache.DirectRealPath.Equals(directRealPath, StringComparison.OrdinalIgnoreCase) &&
                DateTime.Equals(scCache.LastWriteTimeUtc, f.LastWriteTimeUtc) &&
                scCache.FileSize == f.Length)
            { // Cache Hit
                try
                {
                    sc = MessagePackSerializer.Deserialize <Script>(scCache.Serialized, _msgPackOpts);

                    // Deserialization failed (Casting failure)
                    // Ex) Field of the `Script` class have changed without revision update
                    if (sc == null)
                    {
                        isCacheValid = false;
                    }
                }
                catch (MessagePackSerializationException)
                { // Exception from MessagePackSerializer.Deserialize
                  // Cache is inconsistent, turn off script cache.
                  // Ex) `Script` class moved to different assembly
                    sc           = null;
                    isCacheValid = false;
                }
            }

            return(sc);
        }
Ejemplo n.º 2
0
        public static (Script sc, bool cacheValid) DeserializeScript(string realPath, CacheModel.ScriptCache[] cachePool)
        {
            Script sc         = null;
            bool   cacheValid = true;

            FileInfo f = new FileInfo(realPath);

            CacheModel.ScriptCache scCache = cachePool.FirstOrDefault(x => x.Hash == realPath.GetHashCode());
            if (scCache != null &&
                scCache.DirectRealPath.Equals(realPath, StringComparison.OrdinalIgnoreCase) &&
                DateTime.Equals(scCache.LastWriteTimeUtc, f.LastWriteTimeUtc) &&
                scCache.FileSize == f.Length)
            { // Cache Hit
                using (MemoryStream ms = new MemoryStream(scCache.Serialized))
                {
                    try
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        sc = formatter.Deserialize(ms) as Script;
                    }
                    catch (SerializationException)
                    { // Exception from BinaryFormatter.Deserialize()
                        // Cache is inconsistent, turn off script cache.
                        // Ex) `Script` class moved to different assembly
                        sc         = null;
                        cacheValid = false;
                    }

                    // Deserialization failed (Casting failure)
                    // Ex) Field of the `Script` class changed without revision update
                    if (sc == null)
                    {
                        cacheValid = false;
                    }
                }
            }

            return(sc, cacheValid);
        }
Ejemplo n.º 3
0
        /// <returns>Return true if cache is updated</returns>
        private bool SerializeScript(Script sc, CacheModel.ScriptCache[] cachePool, List <CacheModel.ScriptCache> updatePool)
        {
            if (cachePool == null)
            {
                throw new ArgumentNullException(nameof(cachePool));
            }
            if (updatePool == null)
            {
                throw new ArgumentNullException(nameof(updatePool));
            }
            Debug.Assert(sc.Type != ScriptType.Directory);

            // If script file is not found in the disk, ignore it
            if (!File.Exists(sc.DirectRealPath))
            {
                return(false);
            }
            FileInfo f = new FileInfo(sc.DirectRealPath);

            // Retrieve Cache
            bool updated = false;

            CacheModel.ScriptCache scCache = cachePool.FirstOrDefault(x => x.Hash == sc.DirectRealPath.GetHashCode());

            // Update Cache into updateDB
            if (scCache == null)
            { // Cache not exists
                scCache = new CacheModel.ScriptCache
                {
                    Hash             = sc.DirectRealPath.GetHashCode(),
                    DirectRealPath   = sc.DirectRealPath,
                    LastWriteTimeUtc = f.LastWriteTimeUtc,
                    FileSize         = f.Length,
                };

                BinaryFormatter formatter = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    formatter.Serialize(ms, sc);
                    scCache.Serialized = ms.ToArray();
                }

                lock (updatePool)
                {
                    updatePool.Add(scCache);
                    updated = true;
                }
            }
            else if (scCache.DirectRealPath.Equals(sc.DirectRealPath, StringComparison.OrdinalIgnoreCase) &&
                     (!DateTime.Equals(scCache.LastWriteTimeUtc, f.LastWriteTimeUtc) || scCache.FileSize != f.Length))
            { // Cache is outdated
                BinaryFormatter formatter = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    formatter.Serialize(ms, sc);
                    scCache.Serialized       = ms.ToArray();
                    scCache.LastWriteTimeUtc = f.LastWriteTimeUtc;
                    scCache.FileSize         = f.Length;
                }

                lock (updatePool)
                {
                    updatePool.Add(scCache);
                    updated = true;
                }
            }

            if (sc.Type == ScriptType.Link && sc.LinkLoaded)
            {
                bool linkUpdated = SerializeScript(sc.Link, cachePool, updatePool);
                updated = updated || linkUpdated;
            }

            return(updated);
        }
Ejemplo n.º 4
0
        /// <returns>Return true if cache was updated</returns>
        private bool SerializeScript(Script sc, List <CacheModel.ScriptCache> updatePool)
        {
            if (updatePool == null)
            {
                throw new ArgumentNullException(nameof(updatePool));
            }
            Debug.Assert(sc.Type != ScriptType.Directory);

            // If script file is not found in the disk, ignore it
            if (!File.Exists(sc.DirectRealPath))
            {
                return(false);
            }
            FileInfo f = new FileInfo(sc.DirectRealPath);

            // Retrieve Cache
            bool updated = false;

            CacheModel.ScriptCache scCache = null;
            if (_cachePool.ContainsKey(sc.DirectRealPath))
            {
                scCache = _cachePool[sc.DirectRealPath];
            }

            // Update Cache into updateDB
            if (scCache == null)
            { // Cache not exists
                scCache = new CacheModel.ScriptCache
                {
                    DirectRealPath   = sc.DirectRealPath,
                    Serialized       = MessagePackSerializer.Serialize(sc, _msgPackOpts),
                    LastWriteTimeUtc = f.LastWriteTimeUtc,
                    FileSize         = f.Length,
                };

                lock (updatePool)
                {
                    updatePool.Add(scCache);
                    updated = true;
                }
            }
            else if (scCache.DirectRealPath.Equals(sc.DirectRealPath, StringComparison.OrdinalIgnoreCase) &&
                     (!DateTime.Equals(scCache.LastWriteTimeUtc, f.LastWriteTimeUtc) || scCache.FileSize != f.Length))
            { // Cache entry is outdated, update the entry.
                scCache.Serialized       = MessagePackSerializer.Serialize(sc, _msgPackOpts);
                scCache.LastWriteTimeUtc = f.LastWriteTimeUtc;
                scCache.FileSize         = f.Length;

                lock (updatePool)
                {
                    updatePool.Add(scCache);
                    updated = true;
                }
            }

            // Serialize also linked scripts
            if (sc.Type == ScriptType.Link && sc.LinkLoaded)
            {
                bool linkUpdated = SerializeScript(sc.Link, updatePool);
                updated = updated || linkUpdated;
            }

            return(updated);
        }