public static ShortUrl GetShortUrlByUrl(string url) { var cachedUrl = _Cache[url] as ShortUrl; if(cachedUrl == null) { var context = new WSDataContext(); cachedUrl = context.ShortUrls.Where(m => m.Url == url).SingleOrDefault(); if (cachedUrl == null) { cachedUrl = new ShortUrl() { Url = url, CreatedDate = DateTime.Now, LastUpdatedDate = DateTime.Now }; context.ShortUrls.Add(cachedUrl); context.SaveChanges(); _Cache.Add(url, cachedUrl, new CacheItemPolicy()); } } return cachedUrl; }
public async Task CreateAsync(ShortUrl shortUrl) { if (shortUrl == null) { throw new ArgumentNullException("shortUrl"); } using (WSDataContext context = new WSDataContext()) { Context.ShortUrls.Add(shortUrl); var result = await Context.SaveChangesAsync(); } }