1 module hunt.cache.ucache; 2 3 import hunt.cache.memory; 4 import hunt.cache.memcached; 5 import hunt.cache.redis; 6 import hunt.cache.rocksdb; 7 import hunt.cache.cache; 8 import hunt.cache.nullable; 9 10 import std.conv; 11 import std.stdio; 12 import std.traits; 13 14 class UCache 15 { 16 //get 17 mixin(genfunc("V get(V)(string key )" , "get!V(key);")); 18 //get_ex 19 mixin(genfunc("Nullable!V get_ex(V)(string key )" , "get_ex!V(key);")); 20 //getall 21 mixin(genfunc("Nullable!V[string] getall(V)(string[] keys)" , "getall!V(keys);")); 22 mixin(genfunc("string get(string key )" , "get!string(key);")); 23 //get_ex 24 mixin(genfunc("Nullable!string get_ex(string key )" , "get_ex!string(key);")); 25 //getall 26 mixin(genfunc("Nullable!string[string] getall(string[] keys)" , "getall!string(keys);")); 27 28 //containsKey 29 mixin(genfunc("bool containsKey(string key)" , "containsKey(key);")); 30 //put 31 mixin(genfunc("void put(V)(string key , V v , uint expired = 0)" , "put!V(key , v , expired);")); 32 //putifabsent 33 mixin(genfunc("bool putifAbsent(V)(string key , V v)" , "putifAbsent!V(key , v);")); 34 //putAll 35 mixin(genfunc("void putAll(V)( V[string] maps , uint expired = 0)" , "putAll!V(maps , expired);")); 36 //remove 37 mixin(genfunc("bool remove(string key)" , "remove(key);")); 38 //removeAll 39 mixin(genfunc("void removeAll(string[] keys)" , "removeAll(keys);")); 40 //clear 41 mixin(genfunc("void clear()" , "clear();")); 42 43 44 alias set = put; 45 alias hasKey = containsKey; 46 mixin(genfunc("void remove(string[] keys)" , "removeAll(keys);")); 47 48 49 static UCache CreateUCache(string driverName = "memory" , 50 string args = "" , bool enableL2 = false ) 51 { 52 switch(driverName) 53 { 54 case "memory": 55 return new UCache(new Cache!MemoryCache(args , enableL2)); 56 version(WITH_HUNT_REDIS) 57 { 58 case "redis": 59 return new UCache(new Cache!RedisCache(args , enableL2)); 60 } 61 version(WITH_HUNT_MEMCACHE) 62 { 63 case "memcached": 64 return new UCache(new Cache!MemcachedCache(args , enableL2)); 65 } 66 version(WITH_HUNT_ROCKSDB) 67 { 68 case "rocksdb": 69 return new UCache(new Cache!RocksdbCache(args , enableL2)); 70 } 71 default: 72 return new UCache(new Cache!MemoryCache(args , enableL2)); 73 } 74 75 76 } 77 78 private: 79 80 static string genfunc(string callorigin , string callfunc) 81 { 82 string f = callorigin ~ " { if(_memory !is null) return _memory." ~ callfunc ; 83 84 version(WITH_HUNT_REDIS) 85 { 86 f ~= " if(_redis !is null) return _redis." ~ callfunc; 87 } 88 89 version(WITH_HUNT_MEMCACHE) 90 { 91 f ~= "if( _memcahed !is null) return _memcahed." ~ callfunc; 92 } 93 94 version(WITH_HUNT_ROCKSDB) 95 { 96 f ~= "if(_rocksdb !is null) return _rocksdb." ~ callfunc ; 97 } 98 99 f ~= "assert(0);}"; 100 return f; 101 } 102 103 this(Object obj) 104 { 105 string className = to!string(typeid(obj)); 106 if(className == to!string(typeid(Cache!MemoryCache))) 107 { 108 _memory = cast(Cache!MemoryCache)obj; 109 return; 110 } 111 version(WITH_HUNT_REDIS) 112 { 113 if(className == to!string(typeid(Cache!RedisCache))) 114 { 115 _redis = cast(Cache!RedisCache)obj; 116 return; 117 } 118 } 119 120 version(WITH_HUNT_MEMCACHE) 121 { 122 if(className == to!string(typeid(Cache!MemcachedCache))) 123 { _memcahed = cast(Cache!MemcachedCache)(obj); 124 return; 125 } 126 } 127 128 version(WITH_HUNT_ROCKSDB){ 129 if(className == to!string(typeid(Cache!RocksdbCache))) 130 { 131 _rocksdb = cast(Cache!RocksdbCache)(obj); 132 return; 133 } 134 } 135 136 assert(0); 137 } 138 139 Cache!MemoryCache _memory = null; 140 version(WITH_HUNT_REDIS) 141 Cache!RedisCache _redis = null; 142 version(WITH_HUNT_MEMCACHE) 143 Cache!MemcachedCache _memcahed = null; 144 version(WITH_HUNT_ROCKSDB) 145 Cache!RocksdbCache _rocksdb = null; 146 };