Example #1
0
            public async Task <Result <SaveToCacheResponse> > SaveToCache(SaveToCacheRequest request)
            {
                const string api      = "api/Factory/SaveToCache";
                var          response = await _owner.PostAsync <SaveToCacheRequest, SaveToCacheResponse>(api, request);

                return(response);
            }
        public SaveToCacheResponse SaveToCache(SaveToCacheRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (request.Descriptor == null)
            {
                throw new ArgumentNullException("Descriptor");
            }

            var cache = ServiceHelper.Cache;

            // First try to load it from the cache, if success, update it. Otherwise, assume it is not there and create a new document

            using (var document = DocumentFactory.LoadFromCache(cache, request.Descriptor.DocumentId))
            {
                if (document != null)
                {
                    // Update it
                    document.UpdateFromDocumentDescriptor(request.Descriptor);
                    document.AutoDeleteFromCache  = false;
                    document.AutoDisposeDocuments = true;
                    document.AutoSaveToCache      = false;
                    document.SaveToCache();
                    return(new SaveToCacheResponse {
                        Document = document
                    });
                }
            }

            // Above failed, create a new one.
            var createOptions = new CreateDocumentOptions();

            createOptions.Descriptor  = request.Descriptor;
            createOptions.Cache       = cache;
            createOptions.UseCache    = cache != null;
            createOptions.CachePolicy = ServiceHelper.CreatePolicy();
            using (var document = DocumentFactory.Create(createOptions))
            {
                if (document == null)
                {
                    throw new InvalidOperationException("Failed to create document");
                }

                CacheController.TrySetCacheUri(document);

                if (ServiceHelper.AutoUpdateHistory)
                {
                    document.History.AutoUpdate = true;
                }

                document.AutoDeleteFromCache  = false;
                document.AutoDisposeDocuments = true;
                document.AutoSaveToCache      = false;
                document.SaveToCache();
                return(new SaveToCacheResponse {
                    Document = document
                });
            }
        }