// API

        public async Task AddAsync <TYPE>(string key, TYPE data)
        {
            // save new version shared and locally
            byte[]            cacheData      = new GeneralFormatter().Serialize(data);
            SharedCacheObject sharedCacheObj = new SharedCacheObject {
                Created = DateTime.UtcNow,
                Key     = key,
                Value   = cacheData,
            };
            await DataProvider.RemoveAsync(key);

            await DataProvider.AddAsync(sharedCacheObj); // save shared cached version

            LocalSharedCacheObject localCacheObj = new LocalSharedCacheObject {
                Created = sharedCacheObj.Created,
                Key     = sharedCacheObj.Key,
                Value   = sharedCacheObj.Value,
            };

            using (ICacheDataProvider localCacheDP = YetaWF.Core.IO.Caching.GetLocalCacheProvider()) {
                await localCacheDP.AddAsync(key, localCacheObj); // save locally cached version
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add a shared object.
        /// </summary>
        /// <remarks>This requires an active Lock using a lock provider.</remarks>
        public async Task AddAsync <TYPE>(string key, TYPE data)
        {
            key = GetKey(key);
            // save new version shared and locally
            SharedCacheObject sharedCacheObj = new SharedCacheObject {
                Created = DateTime.UtcNow,
                Key     = key,
                Value   = new GeneralFormatter().Serialize(data),
            };
            await DataProvider.RemoveAsync(key);

            await DataProvider.AddAsync(sharedCacheObj); // save shared cached version

            StaticCacheObject cachedObj = new StaticCacheObject {
                Key     = key,
                Value   = data,
                Created = sharedCacheObj.Created,
            };

            lock (_lockObject) { // used to protect StaticObjects - local only
                StaticObjects.Remove(key);
                StaticObjects.Add(key, cachedObj);
            }
        }
Esempio n. 3
0
        public async Task <GetObjectInfo <TYPE> > GetAsync <TYPE>(string key)
        {
            // get cached version
            TYPE data = default(TYPE);

            key = GetKey(key);

            StaticCacheObject cachedObj;
            bool localValid;

            lock (_lockObject) { // used to protect StaticObjects - local only
                localValid = StaticObjects.TryGetValue(key, out cachedObj);
            }
            if (!localValid)
            {
                cachedObj = new StaticCacheObject {
                    Key     = key,
                    Value   = default(TYPE),
                    Created = DateTime.MinValue,
                };
            }
            else
            {
                data = (TYPE)cachedObj.Value;
            }
            // get shared cached version
            SharedCacheVersion sharedInfo = await SharedCacheVersionPostgreSQLDataProvider.SharedCacheVersionDP.GetVersionAsync(key);

            if (sharedInfo != null)
            {
                if (sharedInfo.Created != cachedObj.Created)
                {
                    // shared cached version is different, retrieve and save locally
                    SharedCacheObject sharedCacheObj = await DataProvider.GetAsync(key);

                    if (sharedCacheObj == null)
                    {
                        // this shouldn't happen, we just got the shared version
                    }
                    else
                    {
                        data       = new GeneralFormatter().Deserialize <TYPE>(sharedCacheObj.Value);
                        sharedInfo = sharedCacheObj;
                    }
                    cachedObj = new StaticCacheObject {
                        Created = sharedInfo.Created,
                        Key     = sharedInfo.Key,
                        Value   = data,
                    };
                    localValid = true;
                    lock (_lockObject) { // used to protect StaticObjects - local only
                        StaticObjects.Remove(key);
                        StaticObjects.Add(key, cachedObj);
                    }
                }
                else
                {
                    // shared version same as local version
                }
                return(new GetObjectInfo <TYPE> {
                    Success = true,
                    Data = data,
                });
            }
            else
            {
                // there is no shared version
                // no shared cache
                if (!localValid)
                {
                    return(new GetObjectInfo <TYPE> {
                        Success = false
                    });
                }
                else
                {
                    return(new GetObjectInfo <TYPE> {
                        Success = true,
                        Data = data,
                    });
                }
            }
        }
        public async Task <GetObjectInfo <TYPE> > GetAsync <TYPE>(string key)
        {
            // get locally cached version
            GetObjectInfo <LocalSharedCacheObject> localInfo;

            using (ICacheDataProvider localCacheDP = YetaWF.Core.IO.Caching.GetLocalCacheProvider()) {
                localInfo = await localCacheDP.GetAsync <LocalSharedCacheObject>(key);

                if (!localInfo.Success)
                {
                    // no locally cached data
                    localInfo = new GetObjectInfo <LocalSharedCacheObject> {
                        Data = new LocalSharedCacheObject {
                            Created = DateTime.MinValue,
                            Key     = key,
                            Value   = null,
                        },
                        Success = false,
                    };
                }
                // get shared cached version
                SharedCacheVersion sharedInfo = await SharedCacheVersionPostgreSQLDataProvider.SharedCacheVersionDP.GetVersionAsync(key);

                if (sharedInfo != null)
                {
                    if (sharedInfo.Created != localInfo.Data.Created)
                    {
                        // shared cached version is different, retrieve and save locally
                        SharedCacheObject sharedCacheObj = await DataProvider.GetAsync(key);

                        if (sharedCacheObj == null)   // this shouldn't happen, we just got the shared version
                                                      // return the local data instead
                        {
                        }
                        else
                        {
                            LocalSharedCacheObject localCacheObj = new LocalSharedCacheObject {
                                Created = sharedCacheObj.Created,
                                Key     = sharedCacheObj.Key,
                                Value   = sharedCacheObj.Value,
                            };
                            await localCacheDP.AddAsync(key, localCacheObj); // save as locally cached version

                            return(new GetObjectInfo <TYPE> {
                                Success = true,
                                Data = new GeneralFormatter().Deserialize <TYPE>(sharedCacheObj.Value),
                            });
                        }
                    }
                    else
                    {
                        // shared version same as local version
                    }
                }
                else
                {
                    // there is no shared version
                }
                // return the local data
                if (localInfo.Success)
                {
                    return(new GetObjectInfo <TYPE> {
                        Success = true,
                        Data = new GeneralFormatter().Deserialize <TYPE>(localInfo.Data.Value),
                    });
                }
                else
                {
                    return(new GetObjectInfo <TYPE> {
                        Success = false,
                    });
                }
            }
        }