public ContactRepository() { HttpContext context = HttpContext.Current; if(context != null) { if (context.Cache[CacheKey] == null) { var contacts = new Contact[] { new Contact { Id = 1, Name = "Jim Random", CreditCardNumber = "1122-3344-5566-7788" }, new Contact { Id = 2, Name = "Shelby Customer", CreditCardNumber = "1092-8347-5665-7483" } }; context.Cache[CacheKey] = contacts; } } }
public bool PostContact(Contact contact) { bool result = false; try { List<Contact> contacts = new List<Contact>(GetContacts()); contacts.Add(contact); HttpContext.Current.Cache[CacheKey] = contacts.ToArray(); result = true; } catch(Exception) { //Handle error or log issue. } return result; }