55import internal .org .apache .commons .pool .impl .GenericObjectPool ;
66
77import java .io .IOException ;
8+ import java .util .HashMap ;
9+ import java .util .Iterator ;
10+ import java .util .Map ;
811
912import nl .justobjects .pushlet .core .Config ;
1013import nl .justobjects .pushlet .core .ConfigDefs ;
@@ -116,6 +119,7 @@ public Object fromXML(String xml) {
116119 return _xstream .fromXML (xml );
117120 }
118121
122+ //基本操作
119123 public java .util .Set <byte []> keys (String pattern ) {
120124 if (_pool != null ) {
121125 Jedis jedis = null ;
@@ -376,12 +380,12 @@ public Long hset(String hkey, String field, String value) {
376380 }
377381 }
378382
379- public Long hdel (String key , String field ) {
383+ public Long hdel (String hkey , String field ) {
380384 if (_pool != null ) {
381385 Jedis jedis = null ;
382386 try {
383387 jedis = _pool .getResource ();
384- return jedis .hdel (key .getBytes (REDIS_CHARSET ), field .getBytes (REDIS_CHARSET ));
388+ return jedis .hdel (hkey .getBytes (REDIS_CHARSET ), field .getBytes (REDIS_CHARSET ));
385389 } catch (IOException e ) {
386390 throw new JedisConnectionException (e );
387391 } finally {
@@ -396,7 +400,7 @@ public Long hdel(String key, String field) {
396400 ShardedJedis jedis = null ;
397401 try {
398402 jedis = _shardedPool .getResource ();
399- return jedis .hdel (key .getBytes (REDIS_CHARSET ), field .getBytes (REDIS_CHARSET ));
403+ return jedis .hdel (hkey .getBytes (REDIS_CHARSET ), field .getBytes (REDIS_CHARSET ));
400404 } catch (IOException e ) {
401405 throw new JedisConnectionException (e );
402406 } finally {
@@ -444,6 +448,59 @@ public java.util.Map<byte[], byte[]> hgetAll(String hkey) {
444448 }
445449 }
446450
451+ public String hmset (String hkey , java .util .Map <String , String > hash ) {
452+ if (_pool != null ) {
453+ Jedis jedis = null ;
454+ try {
455+ jedis = _pool .getResource ();
456+
457+ Map <byte [], byte []> bhash = new HashMap <byte [], byte []>(hash .size ());
458+ Map .Entry <String , String > entry ;
459+ Iterator <Map .Entry <String , String >> iterator = hash .entrySet ().iterator ();
460+ while (iterator .hasNext ()) {
461+ entry = iterator .next ();
462+ bhash .put (entry .getKey ().getBytes (REDIS_CHARSET ), entry .getValue ().getBytes (REDIS_CHARSET ));
463+ }
464+
465+ return jedis .hmset (hkey .getBytes (REDIS_CHARSET ), bhash );
466+ } catch (IOException e ) {
467+ throw new JedisConnectionException (e );
468+ } finally {
469+ if (jedis != null ) {
470+ try {
471+ _pool .returnResource (jedis );
472+ } catch (Throwable thex ) {
473+ }
474+ }
475+ }
476+ } else {
477+ ShardedJedis jedis = null ;
478+ try {
479+ jedis = _shardedPool .getResource ();
480+
481+ Map <byte [], byte []> bhash = new HashMap <byte [], byte []>(hash .size ());
482+ Map .Entry <String , String > entry ;
483+ Iterator <Map .Entry <String , String >> iterator = hash .entrySet ().iterator ();
484+ while (iterator .hasNext ()) {
485+ entry = iterator .next ();
486+ bhash .put (entry .getKey ().getBytes (REDIS_CHARSET ), entry .getValue ().getBytes (REDIS_CHARSET ));
487+ }
488+
489+ return jedis .hmset (hkey .getBytes (REDIS_CHARSET ), bhash );
490+ } catch (IOException e ) {
491+ throw new JedisConnectionException (e );
492+ } finally {
493+ if (jedis != null ) {
494+ try {
495+ _shardedPool .returnResource (jedis );
496+ } catch (Throwable thex ) {
497+ }
498+ }
499+ }
500+ }
501+
502+ }
503+
447504 public Long hlen (String hkey ) {
448505 if (_pool != null ) {
449506 Jedis jedis = null ;
@@ -806,4 +863,107 @@ public Long lrem(String lkey, int count, String value) {
806863 }
807864 }
808865 }
866+
867+ //Set操作
868+ public Boolean sismember (String skey , String member ) {
869+ if (_pool != null ) {
870+ Jedis jedis = null ;
871+ try {
872+ jedis = _pool .getResource ();
873+ return jedis .sismember (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
874+ } catch (IOException e ) {
875+ throw new JedisConnectionException (e );
876+ } finally {
877+ if (jedis != null ) {
878+ try {
879+ _pool .returnResource (jedis );
880+ } catch (Throwable thex ) {
881+ }
882+ }
883+ }
884+ } else {
885+ ShardedJedis jedis = null ;
886+ try {
887+ jedis = _shardedPool .getResource ();
888+ return jedis .sismember (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
889+ } catch (IOException e ) {
890+ throw new JedisConnectionException (e );
891+ } finally {
892+ if (jedis != null ) {
893+ try {
894+ _shardedPool .returnResource (jedis );
895+ } catch (Throwable thex ) {
896+ }
897+ }
898+ }
899+ }
900+ }
901+
902+ public Long sadd (String skey , String member ) {
903+ if (_pool != null ) {
904+ Jedis jedis = null ;
905+ try {
906+ jedis = _pool .getResource ();
907+ return jedis .sadd (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
908+ } catch (IOException e ) {
909+ throw new JedisConnectionException (e );
910+ } finally {
911+ if (jedis != null ) {
912+ try {
913+ _pool .returnResource (jedis );
914+ } catch (Throwable thex ) {
915+ }
916+ }
917+ }
918+ } else {
919+ ShardedJedis jedis = null ;
920+ try {
921+ jedis = _shardedPool .getResource ();
922+ return jedis .sadd (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
923+ } catch (IOException e ) {
924+ throw new JedisConnectionException (e );
925+ } finally {
926+ if (jedis != null ) {
927+ try {
928+ _shardedPool .returnResource (jedis );
929+ } catch (Throwable thex ) {
930+ }
931+ }
932+ }
933+ }
934+ }
935+
936+ public Long srem (String skey , String member ) {
937+ if (_pool != null ) {
938+ Jedis jedis = null ;
939+ try {
940+ jedis = _pool .getResource ();
941+ return jedis .srem (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
942+ } catch (IOException e ) {
943+ throw new JedisConnectionException (e );
944+ } finally {
945+ if (jedis != null ) {
946+ try {
947+ _pool .returnResource (jedis );
948+ } catch (Throwable thex ) {
949+ }
950+ }
951+ }
952+ } else {
953+ ShardedJedis jedis = null ;
954+ try {
955+ jedis = _shardedPool .getResource ();
956+ return jedis .srem (skey .getBytes (REDIS_CHARSET ), member .getBytes (REDIS_CHARSET ));
957+ } catch (IOException e ) {
958+ throw new JedisConnectionException (e );
959+ } finally {
960+ if (jedis != null ) {
961+ try {
962+ _shardedPool .returnResource (jedis );
963+ } catch (Throwable thex ) {
964+ }
965+ }
966+ }
967+ }
968+ }
809969}
0 commit comments