Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
/* * Cache 接口,实现tag功能,key前缀 * Logs,Memcached 在这里没有公开,只是一些具体的实现,你可以根据已经情况实现一下 * Logs.Debug 表示写debug日志 * Memcached 里面的Get,Set,Remove也需要自已实现 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; public class Caches { private static string GenerateKey(string key) { return "project_name/" + key; } /// <summary> /// 取缓存 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object Get(string key) { key = GenerateKey(key); object obj = Memcached.Get(key); if (obj != null) { Logs.Debug("Cache hit " + key); } return obj; } /// <summary> /// 存缓存 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void Set(string key, object value) { key = GenerateKey(key); Memcached.Caches.Set(key, value); Logs.Debug("Cache set " + key); } /// <summary> /// 存缓存,带Tag,用于做类似命名空间的管理 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="tags"></param> public static void Set(string key, object value, string[] tags) { if (tags.Length == 0) { Set(key, value); } for (int i = 0; i < tags.Count(); i++) { tags[i] = "tags/" + tags[i]; } List<string> tagList = new List<string>(); foreach (string tag in tags) { object tagObj = Get(tag); if (tagObj != null) { tagList = (List<string>)tagObj; } tagList.Add(key); Set(tag, tagList); } Set(key, value); } /// <summary> /// 删缓存 /// </summary> /// <param name="key"></param> public static void Remove(string key) { key = GenerateKey(key); Memcached.Remove(key); Logs.Debug("Cache remove " + key); } /// <summary> /// 根据 Tag 删除缓存 /// </summary> /// <param name="tags"></param> public static void RemoveByTags(string[] tags) { for (int i = 0; i < tags.Count(); i++) { tags[i] = "tags/" + tags[i]; } List<string> tagList = new List<string>(); foreach (string tag in tags) { object tagsObj = Get(tag); if (tagsObj != null) { tagList = (List<string>)tagsObj; foreach (string key in tagList) { Remove(key); } } Remove(tag); } } }
This paste will be private.
From the Design Piracy series on my blog: