Exemplo n.º 1
0
        public override void Store <T>(string key, T value, string group = null)
        {
            var resolved = ResolveKey(key, group);
            var session  = GetSession();

            session[resolved] = new CachingItem <T>(key, value, group);
        }
        public void TestRemove()
        {
            SetUp();

            var site     = Path.Combine("Website1", "web.config");
            var expected = "expected_remove1.site.config";
            var document = XDocument.Load(site);

            document.Save(expected);

            var item = new CachingItem(null);

            item.Extension = ".xls";
            _feature.AddItem(item);

            Assert.Equal(".xls", _feature.SelectedItem.Extension);
            Assert.Equal(2, _feature.Items.Count);
            _feature.Remove();
            Assert.Null(_feature.SelectedItem);
            Assert.Single(_feature.Items);

            const string Original     = @"original.config";
            const string OriginalMono = @"original.mono.config";

            XmlAssert.Equal(Helper.IsRunningOnMono() ? OriginalMono : Original, Current);
            XmlAssert.Equal(expected, site);
        }
        public void TestAdd()
        {
            SetUp();

            var site     = Path.Combine("Website1", "web.config");
            var expected = "expected_add.site.config";
            var document = XDocument.Load(site);
            var node     = document.Root?.XPathSelectElement("/configuration/system.webServer");

            node?.Add(
                new XElement("caching",
                             new XElement("profiles",
                                          new XElement("add",
                                                       new XAttribute("extension", ".ppt"),
                                                       new XAttribute("duration", "00:00:00")))));
            document.Save(expected);

            var item = new CachingItem(null);

            item.Extension = ".ppt";
            _feature.AddItem(item);
            Assert.NotNull(_feature.SelectedItem);
            Assert.Equal(".ppt", _feature.SelectedItem.Extension);

            const string Original     = @"original.config";
            const string OriginalMono = @"original.mono.config";

            XmlAssert.Equal(Helper.IsRunningOnMono() ? OriginalMono : Original, Current);
            XmlAssert.Equal(expected, site);
        }
Exemplo n.º 4
0
        public override T Fetch <T>(string key, string @group = null, Func <string, string, T> factory = null)
        {
            string resolved = ResolveKey(key, group);

            var context = GetContext();

            var item = context.Cache[resolved] as CachingItem <T>;

            if (item != null)
            {
                item.LastAccessed = DateTime.UtcNow;

                return(item.Value);
            }

            if (factory != null)
            {
                T value = factory(key, group);
                item = new CachingItem <T>(key, value, group);

                context.Cache[resolved] = item;

                return(value);
            }

            return(default(T));
        }
Exemplo n.º 5
0
        public override void Store <T>(string key, T value, string @group = null)
        {
            var resolved = ResolveKey(key, group);
            var context  = GetContext();
            var item     = new CachingItem <T>(key, value, group);

            context.Cache[resolved] = item;
        }
Exemplo n.º 6
0
        public override IEnumerable <IPropertyDescriptor> GetProperties(Type type, object container)
        {
            CachingItem ci = Cache.GetOrAdd(type, t => CachingItem.Create(t, _resolver));

            if (ci == null)
            {
                throw new NotSupportedException($"Type {type.FullName} is invisible.");
            }
            return(ci.Properies);
        }
Exemplo n.º 7
0
        public override void StoreAll <T>(Func <string, T, int, string> keyFactory, IEnumerable <T> values, string @group = null)
        {
            var context = GetContext();
            var index   = 0;

            foreach (T value in values)
            {
                var key      = keyFactory(group, value, index);
                var resolved = ResolveKey(key, group);
                var item     = new CachingItem <T>(key, value, group);

                context.Cache[resolved] = item;
                index++;
            }
        }
Exemplo n.º 8
0
        public void TestAdd()
        {
            SetUp();
            var item = new CachingItem(null);

            item.Extension = ".ppt";
            _feature.AddItem(item);
            Assert.NotNull(_feature.SelectedItem);
            Assert.Equal(".ppt", _feature.SelectedItem.Extension);

            const string Original     = @"original.config";
            const string OriginalMono = @"original.mono.config";

            XmlAssert.Equal(Helper.IsRunningOnMono() ? OriginalMono : Original, Current);
            XmlAssert.Equal(Path.Combine("Caching", "expected_add.site.config"), Path.Combine("Website1", "web.config"));
        }
Exemplo n.º 9
0
        public override IPropertyDescriptor GetProperty(Type type, object container, string name)
        {
            CachingItem ci = Cache.GetOrAdd(Tuple.Create(type, _resolver.GetType()), pair => CachingItem.Create(pair.Item1, _resolver));

            if (ci.Error != null)
            {
                throw ci.Error;
            }
            if (ci.ExtensibleProperies.Count == 0)
            {
                return(null);
            }
            return((from ep in ci.ExtensibleProperies
                    where name.StartsWith(ep.Prefix)
                    select ep.SetName(name)).FirstOrDefault());
        }
Exemplo n.º 10
0
        public override void StoreAll <T>(Func <string, T, int, string> keyFactory, IEnumerable <T> values,
                                          string group = null)
        {
            var session = GetSession();

            var index = 0;

            foreach (var value in values)
            {
                var key      = keyFactory(group, value, index);
                var resolved = ResolveKey(key, group);

                session[resolved] = new CachingItem <T>(key, value, group);

                index++;
            }
        }
Exemplo n.º 11
0
        public override IEnumerable <T> FetchAll <T>(string group = null, Func <string, T, int, string> keyFactory = null,
                                                     Func <string, IEnumerable <T> > itemFactory = null)
        {
            var session = GetSession();

            var items = session
                        .OfType <string>()
                        .Select(k => session[k])
                        .OfType <CachingItem <T> >()
                        .Where(t => t.Group == group)
                        .ToList();

            if (items.Any())
            {
                DateTime lastAccessed = DateTime.UtcNow;
                items.ForEach(i => i.LastAccessed = lastAccessed);

                return(items.Select(i => i.Value));
            }

            if (itemFactory != null)
            {
                if (keyFactory == null)
                {
                    throw new ArgumentException("A key factory must be provided if an item factory is set.");
                }

                var values = itemFactory(group);
                var index  = 0;

                foreach (var value in values)
                {
                    var key      = keyFactory(group, value, index);
                    var resolved = ResolveKey(key, group);

                    session[resolved] = new CachingItem <T>(key, value, group);
                    index++;
                }

                return(values);
            }

            return(Enumerable.Empty <T>());
        }
Exemplo n.º 12
0
        public async void TestAdd()
        {
            await this.SetUp();

            const string Expected     = @"expected_add.config";
            const string ExpectedMono = @"expected_add.mono.config";

            var item = new CachingItem(null);

            item.Extension = ".txt";
            _feature.AddItem(item);
            Assert.NotNull(_feature.SelectedItem);
            Assert.Equal(".txt", _feature.SelectedItem.Extension);
            Assert.Equal(2, _feature.Items.Count);
            XmlAssert.Equal(
                Helper.IsRunningOnMono()
                    ? Path.Combine("Caching", ExpectedMono)
                    : Path.Combine("Caching", Expected),
                Current);
        }
Exemplo n.º 13
0
        public override IEnumerable <IPropertyDescriptor> GetProperties(Type type, object container)
        {
            CachingItem ci = Cache.GetOrAdd(Tuple.Create(type, _resolver.GetType()), pair => CachingItem.Create(pair.Item1, _resolver));

            if (ci.Error != null)
            {
                throw ci.Error;
            }
            IEnumerable <IPropertyDescriptor> result = ci.Properies;

            if (container != null && ci.ExtensibleProperies.Count > 0)
            {
                foreach (var ep in ci.ExtensibleProperies)
                {
                    result = result.Concat(
                        from key in ep.GetAllKeys(container) ?? Enumerable.Empty <string>()
                        select ep.SetName(ep.Prefix + key));
                }
            }
            return(result);
        }
        public void TestAdd()
        {
            SetUp();
            const string Expected = @"expected_add.config";
            var          document = XDocument.Load(Current);
            var          node     = document.Root.XPathSelectElement("/configuration/system.webServer/caching/profiles/add");
            var          newNode  = new XElement("add");

            newNode.SetAttributeValue("duration", "00:00:00");
            newNode.SetAttributeValue("extension", ".txt");
            node?.AddAfterSelf(newNode);
            document.Save(Expected);

            var item = new CachingItem(null);

            item.Extension = ".txt";
            _feature.AddItem(item);
            Assert.NotNull(_feature.SelectedItem);
            Assert.Equal(".txt", _feature.SelectedItem.Extension);
            Assert.Equal(2, _feature.Items.Count);
            XmlAssert.Equal(Expected, Current);
        }
Exemplo n.º 15
0
        public void TestEdit()
        {
            SetUp();

            var site          = Path.Combine("Website1", "web.config");
            var expected      = "expected_edit1.site.config";
            var document      = XDocument.Load(site);
            var node          = document.Root?.XPathSelectElement("/configuration/system.webServer");
            var security      = new XElement("caching");
            var authorization = new XElement("profiles");
            var add           = new XElement("add");

            add.SetAttributeValue("duration", "00:00:00");
            add.SetAttributeValue("extension", ".xslt");
            node?.Add(security);
            security.Add(authorization);
            authorization.Add(add);
            document.Save(expected);

            var item = new CachingItem(null);

            item.Extension = ".xls";
            _feature.AddItem(item);

            Assert.Equal(".xls", _feature.SelectedItem.Extension);
            Assert.Equal(2, _feature.Items.Count);
            item.Extension = ".xslt";
            _feature.EditItem(item);
            Assert.NotNull(_feature.SelectedItem);
            Assert.Equal(".xslt", _feature.SelectedItem.Extension);
            Assert.Equal(2, _feature.Items.Count);

            const string Original     = @"original.config";
            const string OriginalMono = @"original.mono.config";

            XmlAssert.Equal(Helper.IsRunningOnMono() ? OriginalMono : Original, Current);
            XmlAssert.Equal(expected, site);
        }
Exemplo n.º 16
0
        public void Set(TKey key, TValue value, DateTime expired)
        {
            lock (_mapping.SyncRoot)
            {
                if (_mapping.ContainsKey(key))
                {
                    var item = _mapping[key] as CachingItem <TKey, TValue>;
                    item.Values  = value;
                    item.Expired = expired;
                    _linklist.MarkUse(item.Node);
                }
                else
                {
                    var item = new CachingItem <TKey, TValue>()
                    {
                        Values = value, Expired = expired, Node = _linklist.AddNew(key)
                    };
                    _mapping.Add(key, item);
                }

                //Console.WriteLine("LRUCaching:" + _mapping.Keys.Count);
            }
        }
Exemplo n.º 17
0
            public static CachingItem Create(Type type, ITypeResolver typeResolver)
            {
                if (!type.IsVisible)
                {
                    return(null);
                }

                var result = new CachingItem();

                foreach (var prop in type.GetPublicProperties())
                {
                    if (prop.GetIndexParameters().Length > 0)
                    {
                        continue;
                    }
                    var getMethod = prop.GetGetMethod();
                    if (getMethod == null)
                    {
                        continue;
                    }
                    var setMethod    = prop.GetSetMethod();
                    var propertyType = prop.PropertyType;
                    result.Properies.Add(new EmitPropertyDescriptor
                    {
                        CanWrite     = setMethod != null,
                        Name         = prop.Name,
                        Property     = prop,
                        Type         = propertyType,
                        TypeResolver = typeResolver,
                        Reader       = CreateReader(getMethod),
                        Writer       = setMethod == null ? null : CreateWriter(setMethod),
                    });
                }

                return(result);
            }
Exemplo n.º 18
0
        public override T Fetch <T>(string key, string group = null, Func <string, string, T> factory = null)
        {
            var resolved = ResolveKey(key, group);
            var session  = GetSession();
            var item     = session[resolved] as CachingItem <T>;

            if (item != null)
            {
                item.LastAccessed = DateTime.UtcNow;
                session[resolved] = item;

                return(item.Value);
            }

            if (factory != null)
            {
                T value = factory(key, group);
                session[resolved] = new CachingItem <T>(key, value, group);

                return(value);
            }

            return(default(T));
        }
Exemplo n.º 19
0
            public static CachingItem Create(Type type)
            {
                var result = new CachingItem();

                if (!type.IsVisible)
                {
                    result.Error = new YamlException($"Type {type.FullName} is invisible.");
                    return(result);
                }
                foreach (var prop in type.GetPublicProperties())
                {
                    if (prop.GetIndexParameters().Length > 0)
                    {
                        continue;
                    }
                    var getMethod = prop.GetGetMethod();
                    if (getMethod == null)
                    {
                        continue;
                    }
                    var propertyType = prop.PropertyType;
                    var extAttr      = prop.GetCustomAttribute <ExtensibleMemberAttribute>();
                    if (extAttr == null)
                    {
                        var setMethod = prop.GetSetMethod();
                        result.Properies.Add(new EmitPropertyDescriptorSkeleton
                        {
                            CanWrite = setMethod != null,
                            Name     = prop.Name,
                            Property = prop,
                            Type     = propertyType,
                            Reader   = CreateReader(getMethod),
                            Writer   = setMethod == null ? null : CreateWriter(setMethod),
                        });
                    }
                    else
                    {
                        Type valueType = GetGenericValueType(propertyType);

                        if (valueType == null)
                        {
                            result.Error = new YamlException($"Extensible property {prop.Name} in type {type.FullName} do NOT implement IDictionary<string, ?>");
                            return(result);
                        }

                        result.ExtensibleProperies.Add(
                            new ExtensiblePropertyDescriptorSkeleton
                        {
                            KeyReader = CreateDictionaryKeyReader(getMethod, valueType),
                            Prefix    = extAttr.Prefix,
                            Reader    = CreateDictionaryReader(getMethod, valueType),
                            Writer    = CreateDictionaryWriter(getMethod, valueType),
                            Type      = valueType,
                        });
                    }
                }

                // order by the length of Prefix descending.
                result.ExtensibleProperies.Sort((left, right) => right.Prefix.Length - left.Prefix.Length);
                return(result);
            }