Skip to content

Instantly share code, notes, and snippets.

@uniquelau
Created December 3, 2015 15:01
Show Gist options
  • Save uniquelau/613181efb76f8c44890f to your computer and use it in GitHub Desktop.
Save uniquelau/613181efb76f8c44890f to your computer and use it in GitHub Desktop.
Insta Cache
public class InstaService
{
/// <summary>
/// Handles Connection to Instagram, configuration values are set in the web.config AppSettings element
/// </summary>
private readonly InstagramOAuthData _instagram;
private readonly InstagramService _service;
public InstaService()
{
_instagram = Settings.GetInstance().OAuthInstgram;
if (_instagram != null && _instagram.IsValid) {
_service = _instagram.GetService();
}
}
/// <summary>
/// Wrapper for Skybrud Social Instagram - Handles Authentication, pulling values from the web.config
/// </summary>
/// <returns>Skybrud.Social.TwitterService</returns>
public InstagramService Service()
{
return _service;
}
public IEnumerable<InstagramMedia> GetPostsForUser()
{
var cache = MemoryCache.Default;
var alias = _service.Users.GetSelf().Body.Data.Id;
var cacheKey = String.Format("insta-{0}", alias);
if (cache.Get(cacheKey) != null)
{
return (IEnumerable<InstagramMedia>)cache.Get(cacheKey);
}
InstagramRecentMediaResponse response = _service.Users.GetRecentMedia();
var posts = response.Body.Data;
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(1) };
cache.AddOrGetExisting(cacheKey, posts, policy);
return posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment