@@ -257,6 +257,16 @@ public void setCompressEnable(boolean compressEnable) {
257257 public void setCompressThreshold (long compressThreshold ) {
258258 this .compressThreshold = compressThreshold ;
259259 }
260+
261+ /**
262+ * Checks to see if key exists in cache.
263+ *
264+ * @param key the key to look for
265+ * @return true if key found in cache, false if not (or if cache is down)
266+ */
267+ public boolean keyExists ( String key ) {
268+ return ( this .get ( key ) != null );
269+ }
260270
261271 /**
262272 * Deletes an object from cache given cache key.
@@ -531,9 +541,9 @@ private boolean set(String cmdname, String key, Object value, Date expiry, Integ
531541 // byte array to hold data
532542 byte [] val ;
533543
534- if (NativeHandler .isHandled (value ) ) {
544+ if ( NativeHandler .isHandled ( value ) ) {
535545
536- if (asString ) {
546+ if ( asString ) {
537547 // useful for sharing data between java and non-java
538548 // and also for storing ints for the increment method
539549 log .info ("++++ storing data as a string for key: " + key + " for class: " + value .getClass ().getName ());
@@ -709,7 +719,7 @@ public long getCounter(String key) {
709719 public long getCounter (String key , Integer hashCode ) {
710720 long counter = -1 ;
711721 try {
712- counter = ((Long ) get (key , hashCode )).longValue ();
722+ counter = ((Long )get ( key , hashCode , true )).longValue ();
713723 }
714724 catch (Exception ex ) {
715725 // not found or error getting out
@@ -864,7 +874,7 @@ private long incrdecr(String cmdname, String key, long inc, Integer hashCode) {
864874 * @return the object that was previously stored, or null if it was not previously stored
865875 */
866876 public Object get (String key ) {
867- return get (key , null );
877+ return get ( key , null , false );
868878 }
869879
870880 /**
@@ -880,7 +890,7 @@ public Object get(String key) {
880890 * @param hashCode if not null, then the int hashcode to use
881891 * @return the object that was previously stored, or null if it was not previously stored
882892 */
883- public Object get (String key , Integer hashCode ) {
893+ public Object get ( String key , Integer hashCode , boolean asString ) {
884894
885895 // get SockIO obj using cache key
886896 SockIOPool .SockIO sock = SockIOPool .getInstance ().getSock (key , hashCode );
@@ -892,13 +902,13 @@ public Object get(String key, Integer hashCode) {
892902 String cmd = "get " + key + "\r \n " ;
893903 log .debug ("++++ memcache get command: " + cmd );
894904
895- sock .write (cmd .getBytes ());
905+ sock .write ( cmd .getBytes () );
896906 sock .flush ();
897907
898908 // build empty map
899909 // and fill it from server
900910 Map hm = new HashMap ();
901- loadItems (sock , hm );
911+ loadItems ( sock , hm , asString );
902912
903913 // debug code
904914 log .debug ("++++ memcache: got back " + hm .size () + " results" );
@@ -938,8 +948,8 @@ public Object get(String key, Integer hashCode) {
938948 * @param keys String array of keys to retrieve
939949 * @return Object array ordered in same order as key array containing results
940950 */
941- public Object [] getMultiArray (String [] keys ) {
942- return getMultiArray (keys , null );
951+ public Object [] getMultiArray ( String [] keys ) {
952+ return getMultiArray ( keys , null );
943953 }
944954
945955 /**
@@ -952,9 +962,9 @@ public Object[] getMultiArray(String[] keys) {
952962 * @param hashCodes if not null, then the Integer array of hashCodes
953963 * @return Object array ordered in same order as key array containing results
954964 */
955- public Object [] getMultiArray (String [] keys , Integer [] hashCodes ) {
965+ public Object [] getMultiArray ( String [] keys , Integer [] hashCodes ) {
956966
957- Map data = getMulti (keys , hashCodes );
967+ Map data = getMulti ( keys , hashCodes , false );
958968
959969 Object [] res = new Object [keys .length ];
960970 for (int i = 0 ; i < keys .length ; i ++) {
@@ -975,8 +985,8 @@ public Object[] getMultiArray(String[] keys, Integer[] hashCodes) {
975985 * keys that are not found are not entered into the hashmap, but attempting to
976986 * retrieve them from the hashmap gives you null.
977987 */
978- public Map getMulti (String [] keys ) {
979- return getMulti (keys , null );
988+ public Map getMulti ( String [] keys ) {
989+ return getMulti ( keys , null , false );
980990 }
981991
982992 /**
@@ -991,15 +1001,14 @@ public Map getMulti(String[] keys) {
9911001 * keys that are not found are not entered into the hashmap, but attempting to
9921002 * retrieve them from the hashmap gives you null.
9931003 */
994- public Map getMulti (String [] keys , Integer [] hashCodes ) {
1004+ public Map getMulti ( String [] keys , Integer [] hashCodes , boolean asString ) {
9951005 Map sockKeys = new HashMap ();
9961006
9971007 for (int i = 0 ; i < keys .length ; ++i ) {
9981008
9991009 Integer hash = null ;
1000- if (hashCodes != null && hashCodes .length > i ) {
1010+ if ( hashCodes != null && hashCodes .length > i )
10011011 hash = hashCodes [i ];
1002- }
10031012
10041013 // get SockIO obj from cache key
10051014 SockIOPool .SockIO sock = SockIOPool .getInstance ().getSock (keys [i ], hash );
@@ -1008,9 +1017,8 @@ public Map getMulti(String[] keys, Integer[] hashCodes) {
10081017 continue ;
10091018
10101019 // store in map and list if not already
1011- if (!sockKeys .containsKey (sock .getHost ())) {
1012- sockKeys .put (sock .getHost (), new StringBuffer ());
1013- }
1020+ if ( !sockKeys .containsKey ( sock .getHost () ) )
1021+ sockKeys .put ( sock .getHost (), new StringBuffer () );
10141022
10151023 ((StringBuffer ) sockKeys .get (sock .getHost ())).append (" " + keys [i ]);
10161024
@@ -1028,11 +1036,11 @@ public Map getMulti(String[] keys, Integer[] hashCodes) {
10281036 SockIOPool .SockIO sock = SockIOPool .getInstance ().getConnection (host );
10291037
10301038 try {
1031- String cmd = "get" + (StringBuffer ) sockKeys .get (host ) + "\r \n " ;
1032- log .debug ("++++ memcache getMulti cmd: " + cmd );
1033- sock .write (cmd .getBytes ());
1039+ String cmd = "get" + (StringBuffer ) sockKeys .get ( host ) + "\r \n " ;
1040+ log .debug ( "++++ memcache getMulti cmd: " + cmd );
1041+ sock .write ( cmd .getBytes () );
10341042 sock .flush ();
1035- loadItems (sock , ret );
1043+ loadItems ( sock , ret , asString );
10361044 }
10371045 catch (IOException e ) {
10381046 // exception thrown
@@ -1068,12 +1076,14 @@ public Map getMulti(String[] keys, Integer[] hashCodes) {
10681076 *
10691077 * @param sock socket waiting to pass back data
10701078 * @param hm hashmap to store data into
1079+ * @param asString if true, and if we are using NativehHandler, return string val
10711080 * @throws IOException if io exception happens while reading from socket
10721081 */
1073- private void loadItems (SockIOPool .SockIO sock , Map hm ) throws IOException {
1074- while (true ) {
1082+ private void loadItems ( SockIOPool .SockIO sock , Map hm , boolean asString ) throws IOException {
1083+
1084+ while ( true ) {
10751085 String line = sock .readLine ();
1076- log .debug ("++++ line: " + line );
1086+ log .debug ( "++++ line: " + line );
10771087
10781088 if (line .startsWith (VALUE )) {
10791089 String [] info = line .split (" " );
@@ -1121,10 +1131,10 @@ private void loadItems(SockIOPool.SockIO sock, Map hm) throws IOException {
11211131
11221132 // we can only take out serialized objects
11231133 if ((flag & F_SERIALIZED ) == 0 ) {
1124- if (primitiveAsString ) {
1134+ if ( primitiveAsString || asString ) {
11251135 // pulling out string value
11261136 log .info ("++++ retrieving object and stuffing into a string." );
1127- o = new String (buf , defaultEncoding );
1137+ o = new String ( buf , defaultEncoding );
11281138 }
11291139 else {
11301140 // decoding object
@@ -1139,7 +1149,7 @@ private void loadItems(SockIOPool.SockIO sock, Map hm) throws IOException {
11391149 }
11401150 else {
11411151 // deserialize if the data is serialized
1142- ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (buf ) );
1152+ ObjectInputStream ois = new ObjectInputStream ( new ByteArrayInputStream ( buf ) );
11431153 try {
11441154 o = ois .readObject ();
11451155 log .info ("++++ deserializing " + o .getClass ());
@@ -1151,9 +1161,9 @@ private void loadItems(SockIOPool.SockIO sock, Map hm) throws IOException {
11511161 }
11521162
11531163 // store the object into the cache
1154- hm .put (key , o );
1164+ hm .put ( key , o );
11551165 }
1156- else if (END .equals (line ) ) {
1166+ else if ( END .equals ( line ) ) {
11571167 log .debug ("++++ finished reading from cache server" );
11581168 break ;
11591169 }
0 commit comments