public async Task<IHttpActionResult> Get()
        {
            var cache = RedisCache.Connection.GetDatabase();
            var repo = new RedisRepository(cache);

            string owner = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

            var items = await repo.GetPhotosForUserAsync(owner);
            //Do this so we can get the count
            List<IPhotoModel> typedItems = new List<IPhotoModel>(items);
            
            if(typedItems.Count == 0)
            {
                //Nothing in cache... head off to storage.
                var storageRepo = new StorageRepository(SettingsHelper.LocalStorageConnectionString);
                
                var photos = await storageRepo.GetPhotosFromTableAsync( DAL.Azure.StorageConfig.TableName, owner);
                foreach (var photo in photos)
                {
                    //TODO: Find a MUCH better algorithm than
                    //      iterating every item and calling
                    //      Redis 3 times in a row for each
                    //      item.  This is PAINFUL.
                    await repo.AddPhotoToCachesAsync(photo);
                }
                items = photos;                
            }
            return Ok(items);
        }
示例#2
0
 internal static async Task SaveToRedisAsync(PhotoModel p, TextWriter log)
 {
     //Use repository to add to all caches
     IDatabase       cache = RedisCache.Connection.GetDatabase();
     RedisRepository repo  = new RedisRepository(cache);
     await repo.AddPhotoToCachesAsync(p);
 }
示例#3
0
        public async Task <IHttpActionResult> Get()
        {
            var cache = RedisCache.Connection.GetDatabase();
            var repo  = new RedisRepository(cache);

            string owner = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

            var items = await repo.GetPhotosForUserAsync(owner);

            //Do this so we can get the count
            List <IPhotoModel> typedItems = new List <IPhotoModel>(items);

            if (typedItems.Count == 0)
            {
                //Nothing in cache... head off to storage.
                var storageRepo = new StorageRepository(SettingsHelper.LocalStorageConnectionString);

                var photos = await storageRepo.GetPhotosFromTableAsync(DAL.Azure.StorageConfig.TableName, owner);

                foreach (var photo in photos)
                {
                    //TODO: Find a MUCH better algorithm than
                    //      iterating every item and calling
                    //      Redis 3 times in a row for each
                    //      item.  This is PAINFUL.
                    await repo.AddPhotoToCachesAsync(photo);
                }
                items = photos;
            }
            return(Ok(items));
        }
示例#4
0
        internal static async Task SaveToRedisAsync(PhotoModel p, TextWriter log)
        {
            //Use repository to add to all caches
            IDatabase cache = RedisCache.Connection.GetDatabase();
            RedisRepository repo = new RedisRepository(cache);
            await repo.AddPhotoToCachesAsync(p);

        }
示例#5
0
        public async Task <IHttpActionResult> GetPhotoModel(string id)
        {
            var cache = RedisCache.Connection.GetDatabase();
            var repo  = new RedisRepository(cache);

            //Get a single item from the cache based on its ID
            var photo = await repo.GetPhotoByIDAsync(id);

            if (null == photo)
            {
                //Not in the cache.  Try to retrieve based on
                //  current user.  This won't work if it's another user's photo.
                if (User.Identity.IsAuthenticated)
                {
                    string connectionString = SettingsHelper.LocalStorageConnectionString;
                    string owner            = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                    var    storageRepo      = new StorageRepository(connectionString);
                    photo = await storageRepo.GetPhotoFromTableAsync(DAL.Azure.StorageConfig.PhotosBlobContainerName, owner, id);

                    if (null == photo)
                    {
                        //Not found in cache or storage.
                    }
                    else
                    {
                        //Update the cache using the cache aside pattern.
                        await repo.AddPhotoToCachesAsync(photo);
                    }
                }
            }
            if (null != photo)
            {
                return(Ok(photo));
            }
            else
            {
                return(NotFound());
            }
        }
        public async Task<IHttpActionResult> GetPhotoModel(string id)
        {            
            var cache = RedisCache.Connection.GetDatabase();
            var repo = new RedisRepository(cache);
            
            //Get a single item from the cache based on its ID
            var photo = await repo.GetPhotoByIDAsync(id);

            if(null == photo)
            {
                //Not in the cache.  Try to retrieve based on 
                //  current user.  This won't work if it's another user's photo.
                if(User.Identity.IsAuthenticated)
                {
                    string connectionString = SettingsHelper.LocalStorageConnectionString;
                    string owner = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                    var storageRepo = new StorageRepository(connectionString);
                    photo = await storageRepo.GetPhotoFromTableAsync(DAL.Azure.StorageConfig.PhotosBlobContainerName, owner, id);
                    if (null == photo)
                    {
                        //Not found in cache or storage.                      
                    }
                    else
                    {                        
                        //Update the cache using the cache aside pattern.
                        await repo.AddPhotoToCachesAsync(photo);
                    }
                }
            }
            if(null != photo)
            {
                return Ok(photo);
            }
            else
            {
                return NotFound();
            }
        }