示例#1
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,
                    });
                }
            }
        }