protected virtual void AddToMetadataCache(IItemMetadata metadata, string path = null)
        {
            var cachedValue = new WrittenItemMetadata(metadata.Id, metadata.ParentId, metadata.TemplateId, metadata.Path, path ?? metadata.SerializedItemId);

            _idCache[metadata.Id] = cachedValue;
            _metadataCache.AddOrUpdate(cachedValue.SerializedItemId, cachedValue);
        }
        protected virtual void WriteItem(IItemData item, string path)
        {
            Assert.ArgumentNotNull(item, "item");
            Assert.ArgumentNotNullOrEmpty(path, "path");

            var proxiedItem = new ProxyItem(item)
            {
                SerializedItemId = path
            };

            lock (FileUtil.GetFileLock(path))
            {
                try
                {
                    _treeWatcher.PushKnownUpdate(path, TreeWatcher.TreeWatcherChangeType.ChangeOrAdd);
                    var directory = Path.GetDirectoryName(path);
                    if (directory != null && !Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    using (var writer = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        _formatter.WriteSerializedItem(proxiedItem, writer);
                    }
                }
                catch (Exception exception)
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    throw new SfsWriteException("Error while writing SFS item " + path, exception);
                }
            }

            AddToMetadataCache(item, path);
            _dataCache.AddOrUpdate(path, proxiedItem);
        }