001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Charles Reich
028: */
029:
030: package com.caucho.quercus.lib;
031:
032: import com.caucho.quercus.annotation.Optional;
033: import com.caucho.quercus.env.BooleanValue;
034: import com.caucho.quercus.env.Env;
035: import com.caucho.quercus.env.Value;
036: import com.caucho.util.L10N;
037: import com.caucho.util.LruCache;
038:
039: import java.util.logging.Logger;
040:
041: /**
042: * memcache object oriented API facade
043: */
044: public class Memcache {
045: private static final Logger log = Logger.getLogger(Memcache.class
046: .getName());
047: private static final L10N L = new L10N(Memcache.class);
048:
049: private Cache _cache;
050:
051: /**
052: * Adds a server.
053: */
054: public boolean addServer(Env env, String host, @Optional
055: int port, @Optional
056: boolean persistent, @Optional
057: int weight, @Optional
058: int timeout, @Optional
059: int retryInterval) {
060: if (_cache == null)
061: connect(env, host, port, timeout);
062:
063: return true;
064: }
065:
066: /**
067: * Connect to a server.
068: */
069: public boolean connect(Env env, String host, @Optional
070: int port, @Optional("1")
071: int timeout) {
072: // Always true since this is a local copy
073:
074: String name = "memcache::" + host + ":" + port;
075:
076: _cache = (Cache) env.getQuercus().getSpecial(name);
077:
078: if (_cache == null) {
079: _cache = new Cache();
080:
081: env.getQuercus().setSpecial(name, _cache);
082: }
083:
084: return true;
085: }
086:
087: /**
088: * Returns a value.
089: */
090: public Value get(Env env, Value keys) {
091: if (keys.isArray())
092: return BooleanValue.FALSE;
093:
094: String key = keys.toString();
095:
096: Value value = _cache.get(key);
097:
098: if (value != null)
099: return value.copy(env);
100: else
101: return BooleanValue.FALSE;
102: }
103:
104: /**
105: * Returns version information.
106: */
107: public String getVersion() {
108: return "1.0";
109: }
110:
111: /**
112: * Connect to a server.
113: */
114: public boolean pconnect(Env env, String host, @Optional
115: int port, @Optional("1")
116: int timeout) {
117: return connect(env, host, port, timeout);
118: }
119:
120: /**
121: * Sets a value.
122: */
123: public boolean set(Env env, String key, Value value, @Optional
124: int flag, @Optional
125: int expire) {
126: _cache.set(key, value.copy(env));
127:
128: return true;
129: }
130:
131: /**
132: * Sets the compression threshold
133: */
134: public boolean setCompressThreshold(int threshold, @Optional
135: double minSavings) {
136: return true;
137: }
138:
139: /**
140: * Closes the connection.
141: */
142: public boolean close() {
143: return true;
144: }
145:
146: public String toString() {
147: return "Memcache[]";
148: }
149:
150: static class Cache extends Value {
151: private LruCache<String, Value> _map = new LruCache<String, Value>(
152: 256);
153:
154: public Value get(String key) {
155: return _map.get(key);
156: }
157:
158: public void set(String key, Value value) {
159: _map.put(key, value);
160: }
161: }
162: }
|