Created
December 3, 2015 15:01
-
-
Save uniquelau/613181efb76f8c44890f to your computer and use it in GitHub Desktop.
Insta Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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