1 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.quercus.annotation.Optional; 33 import com.caucho.quercus.env.BooleanValue; 34 import com.caucho.quercus.env.Env; 35 import com.caucho.quercus.env.Value; 36 import com.caucho.util.L10N; 37 import com.caucho.util.LruCache; 38 39 import java.util.logging.Logger ; 40 41 44 public class Memcache { 45 private static final Logger log = Logger.getLogger(Memcache.class.getName()); 46 private static final L10N L = new L10N(Memcache.class); 47 48 private Cache _cache; 49 50 53 public boolean addServer(Env env, 54 String host, 55 @Optional int port, 56 @Optional boolean persistent, 57 @Optional int weight, 58 @Optional int timeout, 59 @Optional int retryInterval) 60 { 61 if (_cache == null) 62 connect(env, host, port, timeout); 63 64 return true; 65 } 66 67 70 public boolean connect(Env env, 71 String host, 72 @Optional int port, 73 @Optional("1") int timeout) 74 { 75 77 String name = "memcache::" + host + ":" + port; 78 79 _cache = (Cache) env.getQuercus().getSpecial(name); 80 81 if (_cache == null) { 82 _cache = new Cache(); 83 84 env.getQuercus().setSpecial(name, _cache); 85 } 86 87 return true; 88 } 89 90 93 public Value get(Env env, Value keys) 94 { 95 if (keys.isArray()) 96 return BooleanValue.FALSE; 97 98 String key = keys.toString(); 99 100 Value value = _cache.get(key); 101 102 if (value != null) 103 return value.copy(env); 104 else 105 return BooleanValue.FALSE; 106 } 107 108 111 public String getVersion() 112 { 113 return "1.0"; 114 } 115 116 119 public boolean pconnect(Env env, 120 String host, 121 @Optional int port, 122 @Optional("1") int timeout) 123 { 124 return connect(env, host, port, timeout); 125 } 126 127 130 public boolean set(Env env, 131 String key, 132 Value value, 133 @Optional int flag, 134 @Optional int expire) 135 { 136 _cache.set(key, value.copy(env)); 137 138 return true; 139 } 140 141 144 public boolean setCompressThreshold(int threshold, 145 @Optional double minSavings) 146 { 147 return true; 148 } 149 150 153 public boolean close() 154 { 155 return true; 156 } 157 158 public String toString() 159 { 160 return "Memcache[]"; 161 } 162 163 static class Cache extends Value { 164 private LruCache<String ,Value> _map = new LruCache<String ,Value>(256); 165 166 public Value get(String key) 167 { 168 return _map.get(key); 169 } 170 171 public void set(String key, Value value) 172 { 173 _map.put(key, value); 174 } 175 } 176 } 177 | Popular Tags |