KickJava   Java API By Example, From Geeks To Geeks.

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


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 Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.env.*;
34 import com.caucho.quercus.module.AbstractQuercusModule;
35 import com.caucho.util.Alarm;
36 import com.caucho.util.L10N;
37 import com.caucho.util.LruCache;
38
39 import java.util.HashMap JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * APC object oriented API facade
45  */

46 public class ApcModule extends AbstractQuercusModule {
47   private static final Logger JavaDoc log = Logger.getLogger(ApcModule.class.getName());
48   private static final L10N L = new L10N(ApcModule.class);
49
50   private static final HashMap JavaDoc<String JavaDoc,StringValue> _iniMap
51     = new HashMap JavaDoc<String JavaDoc,StringValue>();
52
53   private LruCache<String JavaDoc,Entry> _cache = new LruCache<String JavaDoc,Entry>(4096);
54
55   private HashMap JavaDoc<String JavaDoc,Value> _constMap = new HashMap JavaDoc<String JavaDoc,Value>();
56
57   /**
58    * Returns true for the mysql extension.
59    */

60   public String JavaDoc []getLoadedExtensions()
61   {
62     return new String JavaDoc[] { "apc" };
63   }
64
65   /**
66    * Returns cache information.
67    */

68   public Value apc_cache_info(@Optional String JavaDoc type)
69   {
70     ArrayValue value = new ArrayValueImpl();
71
72     value.put("num_slots", 1000);
73     value.put("ttl", 0);
74     value.put("num_hits", 0);
75     value.put("num_misses", 0);
76     value.put("start_time", 0);
77     value.put(new StringValueImpl("cache_list"), new ArrayValueImpl());
78
79     return value;
80   }
81
82   /**
83    * Clears the cache
84    */

85   public boolean apc_clear_cache(Env env, @Optional String JavaDoc type)
86   {
87     _cache.clear();
88
89     return true;
90   }
91
92   /**
93    * Defines constants
94    */

95   public boolean apc_define_constants(Env env,
96                                       String JavaDoc key,
97                                       ArrayValue values,
98                                       @Optional("true") boolean caseSensitive)
99   {
100     _constMap.put(key, values.copy(env));
101
102     return true;
103   }
104
105   /**
106    * Deletes a value.
107    */

108   public boolean apc_delete(Env env, String JavaDoc key)
109   {
110     return _cache.remove(key) != null;
111   }
112
113   /**
114    * Returns a value.
115    */

116   public Value apc_fetch(Env env, String JavaDoc key)
117   {
118     Entry entry = _cache.get(key);
119
120     if (entry == null)
121       return BooleanValue.FALSE;
122
123     Value value = entry.getValue();
124
125     if (entry.isValid() && value != null)
126       return value.copy(env);
127     else
128       return BooleanValue.FALSE;
129   }
130
131   /**
132    * Defines constants
133    */

134   public boolean apc_load_constants(Env env,
135                                     String JavaDoc key,
136                                     @Optional("true") boolean caseSensitive)
137   {
138     ArrayValue array = (ArrayValue) _constMap.get(key);
139
140     if (array == null)
141       return false;
142
143     for (Map.Entry JavaDoc<Value,Value> entry : array.entrySet()) {
144       env.addConstant(entry.getKey().toString(),
145                       entry.getValue().copy(env),
146                       ! caseSensitive);
147     }
148
149     return true;
150   }
151
152   /**
153    * Returns cache information.
154    */

155   public Value apc_sma_info(@Optional String JavaDoc type)
156   {
157     ArrayValue value = new ArrayValueImpl();
158
159     value.put("num_seg", 1);
160     value.put("seg_size", 1024 * 1024);
161     value.put("avail_mem", 1024 * 1024);
162     value.put(new StringValueImpl("block_lists"), new ArrayValueImpl());
163
164     return value;
165   }
166
167   /**
168    * Returns a value.
169    */

170   public Value apc_store(Env env, String JavaDoc key, Value value,
171                          @Optional("0") int ttl)
172   {
173     _cache.put(key, new Entry(value.copy(env), ttl));
174
175     return BooleanValue.TRUE;
176   }
177
178   static class Entry {
179     private Value _value;
180     private long _expire;
181
182     Entry(Value value, int ttl)
183     {
184       _value = value;
185
186       if (ttl <= 0)
187         _expire = Long.MAX_VALUE;
188       else
189         _expire = Alarm.getCurrentTime() + ttl * 1000L;
190     }
191
192     public boolean isValid()
193     {
194       if (Alarm.getCurrentTime() <= _expire)
195         return true;
196       else {
197         _value = null;
198         return false;
199       }
200     }
201
202     public Value getValue()
203     {
204       return _value;
205     }
206   }
207
208   /**
209    * Returns the default quercus.ini values.
210    */

211   public Map JavaDoc<String JavaDoc,StringValue> getDefaultIni()
212   {
213     return _iniMap;
214   }
215
216   static {
217     addIni(_iniMap, "apc.enabled", "1", PHP_INI_ALL);
218     addIni(_iniMap, "apc.shm_segments", "1", PHP_INI_SYSTEM);
219     addIni(_iniMap, "apc.shm_size", "30", PHP_INI_SYSTEM);
220     addIni(_iniMap, "apc.optimization", "0", PHP_INI_ALL);
221     addIni(_iniMap, "apc.num_files_hint", "1000", PHP_INI_SYSTEM);
222     addIni(_iniMap, "apc.ttl", "0", PHP_INI_SYSTEM);
223     addIni(_iniMap, "apc.gc_ttl", "3600", PHP_INI_SYSTEM);
224     addIni(_iniMap, "apc.cache_by_default", "1", PHP_INI_SYSTEM);
225     addIni(_iniMap, "apc.filters", "", PHP_INI_SYSTEM);
226     addIni(_iniMap, "apc.mmap_file_mask", "", PHP_INI_SYSTEM);
227     addIni(_iniMap, "apc.slam_defense", "0", PHP_INI_SYSTEM);
228     addIni(_iniMap, "apc.file_update_protection", "2", PHP_INI_SYSTEM);
229     addIni(_iniMap, "apc.enable_cli", "0", PHP_INI_SYSTEM);
230   }
231 }
232
Popular Tags