KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > Memcache


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Charles Reich
28  */

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 JavaDoc;
40
41 /**
42  * memcache object oriented API facade
43  */

44 public class Memcache {
45   private static final Logger JavaDoc log = Logger.getLogger(Memcache.class.getName());
46   private static final L10N L = new L10N(Memcache.class);
47
48   private Cache _cache;
49
50   /**
51    * Adds a server.
52    */

53   public boolean addServer(Env env,
54                            String JavaDoc 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   /**
68    * Connect to a server.
69    */

70   public boolean connect(Env env,
71                          String JavaDoc host,
72                          @Optional int port,
73                          @Optional("1") int timeout)
74   {
75     // Always true since this is a local copy
76

77     String JavaDoc 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   /**
91    * Returns a value.
92    */

93   public Value get(Env env, Value keys)
94   {
95     if (keys.isArray())
96       return BooleanValue.FALSE;
97
98     String JavaDoc 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   /**
109    * Returns version information.
110    */

111   public String JavaDoc getVersion()
112   {
113     return "1.0";
114   }
115
116   /**
117    * Connect to a server.
118    */

119   public boolean pconnect(Env env,
120                           String JavaDoc host,
121                           @Optional int port,
122                           @Optional("1") int timeout)
123   {
124     return connect(env, host, port, timeout);
125   }
126
127   /**
128    * Sets a value.
129    */

130   public boolean set(Env env,
131                      String JavaDoc 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   /**
142    * Sets the compression threshold
143    */

144   public boolean setCompressThreshold(int threshold,
145                                       @Optional double minSavings)
146   {
147     return true;
148   }
149
150   /**
151    * Closes the connection.
152    */

153   public boolean close()
154   {
155     return true;
156   }
157
158   public String JavaDoc toString()
159   {
160     return "Memcache[]";
161   }
162
163   static class Cache extends Value {
164     private LruCache<String JavaDoc,Value> _map = new LruCache<String JavaDoc,Value>(256);
165
166     public Value get(String JavaDoc key)
167     {
168       return _map.get(key);
169     }
170
171     public void set(String JavaDoc key, Value value)
172     {
173       _map.put(key, value);
174     }
175   }
176 }
177
Popular Tags