예제 #1
0
        private void InvokeMethodOnRefresherInstance <T>(ICacheRefresher refresher, MessageType dispatchType, Func <T, object> getId, IEnumerable <T> instances)
        {
            if (refresher == null)
            {
                throw new ArgumentNullException("refresher");
            }

            LogHelper.Debug <DefaultServerMessenger>("Invoking refresher {0} on single server instance, message type {1}",
                                                     () => refresher.GetType(),
                                                     () => dispatchType);

            var stronglyTypedRefresher = refresher as ICacheRefresher <T>;

            foreach (var instance in instances)
            {
                //if we are not, then just invoke the call on the cache refresher
                switch (dispatchType)
                {
                case MessageType.RefreshAll:
                    refresher.RefreshAll();
                    break;

                case MessageType.RefreshById:
                    if (stronglyTypedRefresher != null)
                    {
                        stronglyTypedRefresher.Refresh(instance);
                    }
                    else
                    {
                        var id = getId(instance);
                        if (id is int)
                        {
                            refresher.Refresh((int)id);
                        }
                        else if (id is Guid)
                        {
                            refresher.Refresh((Guid)id);
                        }
                        else
                        {
                            throw new InvalidOperationException("The id must be either an int or a Guid");
                        }
                    }
                    break;

                case MessageType.RemoveById:
                    if (stronglyTypedRefresher != null)
                    {
                        stronglyTypedRefresher.Remove(instance);
                    }
                    else
                    {
                        var id = getId(instance);
                        refresher.Refresh((int)id);
                    }
                    break;
                }
            }
        }
예제 #2
0
 public bool Create(Feed feed)
 {
     if (_context.Add(feed) != null)
     {
         _context.SaveChanges();
         _cacheRefresher.Refresh();
         return(true);
     }
     return(false);
 }
예제 #3
0
 public bool Create(UserCollection collection)
 {
     if (_context.Add(collection) != null)
     {
         _context.SaveChanges();
         _cacheRefresher.Refresh();
         return(true);
     }
     return(false);
 }
        public Attempt <IUserContent> Delete(IUserContent content)
        {
            if (Deleting.IsRaisedEventCancelled(new DeleteEventArgs <TUserContent>((TUserContent)content), this))
            {
                return(Attempt.Fail <IUserContent>(content, new Exception("blocked by delegated event")));
            }

            _userRepo.Delete(content.Key);

            Deleted.RaiseEvent(new DeleteEventArgs <TUserContent>((TUserContent)content), this);
            _cacheRefresher.Refresh(content.Key);

            return(Attempt.Succeed <IUserContent>(content));
        }
예제 #5
0
        private void RefreshByIds(CacheRefresherCollection cacheRefreshers, Guid uniqueIdentifier, string jsonIds)
        {
            ICacheRefresher refresher = GetRefresher(cacheRefreshers, uniqueIdentifier);

            foreach (var id in JsonConvert.DeserializeObject <int[]>(jsonIds))
            {
                refresher.Refresh(id);
            }
        }
예제 #6
0
        public FeedRepository(ApplicationDbContext context, IMemoryCache cache, ICacheRefresher cacheRefresher)
        {
            _context = context ?? throw new NullReferenceException();

            _cache          = cache;
            _cacheRefresher = cacheRefresher;
            if (!_cache.TryGetValue("Feeds", out List <Feed> feedsCollections))
            {
                var a = _cache.Get("Feeds");
                _cacheRefresher.Refresh();
            }
        }
예제 #7
0
        public CollectionRepository(ApplicationDbContext context, IMemoryCache cache, ICacheRefresher cacheRefresher)
        {
            _context = context ?? throw new NullReferenceException();

            _cache          = cache;
            _cacheRefresher = cacheRefresher;

            if (!_cache.TryGetValue("UserCollections", out List <UserCollection> userCollections))
            {
                _cacheRefresher.Refresh();
            }
        }
예제 #8
0
            private void RefreshByIds(CacheRefresherCollection cacheRefreshers, Guid uniqueIdentifier, string?jsonIds)
            {
                ICacheRefresher refresher = GetRefresher(cacheRefreshers, uniqueIdentifier);

                if (jsonIds is null)
                {
                    return;
                }

                var ids = JsonConvert.DeserializeObject <int[]>(jsonIds);

                if (ids is not null)
                {
                    foreach (var id in ids)
                    {
                        refresher.Refresh(id);
                    }
                }
            }
예제 #9
0
        /// <summary>
        /// Executes the non strongly typed <see cref="ICacheRefresher"/> on the local/current server
        /// </summary>
        /// <param name="refresher"></param>
        /// <param name="messageType"></param>
        /// <param name="ids"></param>
        /// <param name="json"></param>
        /// <remarks>
        /// Since this is only for non strongly typed <see cref="ICacheRefresher"/> it will throw for message types that by instance
        /// </remarks>
        protected void DeliverLocal(ICacheRefresher refresher, MessageType messageType, IEnumerable <object> ids = null, string json = null)
        {
            if (refresher == null)
            {
                throw new ArgumentNullException(nameof(refresher));
            }

            Current.Logger.Debug <ServerMessengerBase, Type, MessageType>("Invoking refresher {RefresherType} on local server for message type {MessageType}", refresher.GetType(), messageType);

            switch (messageType)
            {
            case MessageType.RefreshAll:
                refresher.RefreshAll();
                break;

            case MessageType.RefreshById:
                if (ids != null)
                {
                    foreach (var id in ids)
                    {
                        if (id is int)
                        {
                            refresher.Refresh((int)id);
                        }
                        else if (id is Guid)
                        {
                            refresher.Refresh((Guid)id);
                        }
                        else
                        {
                            throw new InvalidOperationException("The id must be either an int or a Guid.");
                        }
                    }
                }
                break;

            case MessageType.RefreshByJson:
                var jsonRefresher = refresher as IJsonCacheRefresher;
                if (jsonRefresher == null)
                {
                    throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IJsonCacheRefresher));
                }
                jsonRefresher.Refresh(json);
                break;

            case MessageType.RemoveById:
                if (ids != null)
                {
                    foreach (var id in ids)
                    {
                        if (id is int)
                        {
                            refresher.Remove((int)id);
                        }
                        else
                        {
                            throw new InvalidOperationException("The id must be an int.");
                        }
                    }
                }
                break;

            default:
                //case MessageType.RefreshByInstance:
                //case MessageType.RemoveByInstance:
                throw new NotSupportedException("Invalid message type " + messageType);
            }
        }
예제 #10
0
        private void RefreshById(CacheRefresherCollection cacheRefreshers, Guid uniqueIdentifier, int id)
        {
            ICacheRefresher refresher = GetRefresher(cacheRefreshers, uniqueIdentifier);

            refresher.Refresh(id);
        }
        void ProcessRefreshById(Notification notification, ICacheRefresher refresher)
        {
            foreach (var id in _payloadService.Deserialize<object>(notification.Payload))
            {
                if (id is long)
                    refresher.Refresh(Convert.ToInt32((long)id));

                if (id is Guid)
                    refresher.Refresh((Guid)id);
            }
        }
예제 #12
0
        protected void InvokeMethodOnRefresherInstance(ICacheRefresher refresher, MessageType dispatchType, IEnumerable <object> ids = null, string jsonPayload = null)
        {
            if (refresher == null)
            {
                throw new ArgumentNullException("refresher");
            }

            LogHelper.Debug <DefaultServerMessenger>("Invoking refresher {0} on single server instance, message type {1}",
                                                     () => refresher.GetType(),
                                                     () => dispatchType);

            //if it is a refresh all we'll do it here since ids will be null or empty
            if (dispatchType == MessageType.RefreshAll)
            {
                refresher.RefreshAll();
            }
            else
            {
                if (ids != null)
                {
                    foreach (var id in ids)
                    {
                        //if we are not, then just invoke the call on the cache refresher
                        switch (dispatchType)
                        {
                        case MessageType.RefreshById:
                            if (id is int)
                            {
                                refresher.Refresh((int)id);
                            }
                            else if (id is Guid)
                            {
                                refresher.Refresh((Guid)id);
                            }
                            else
                            {
                                throw new InvalidOperationException("The id must be either an int or a Guid");
                            }

                            break;

                        case MessageType.RemoveById:
                            refresher.Remove((int)id);
                            break;
                        }
                    }
                }
                else
                {
                    //we can only proceed if the cache refresher is IJsonCacheRefresher!
                    var jsonRefresher = refresher as IJsonCacheRefresher;
                    if (jsonRefresher == null)
                    {
                        throw new InvalidOperationException("The cache refresher " + refresher.GetType() + " is not of type " + typeof(IJsonCacheRefresher));
                    }

                    //if we are not, then just invoke the call on the cache refresher
                    jsonRefresher.Refresh(jsonPayload);
                }
            }
        }