- 浏览: 103556 次
- 性别:
- 来自: 南京
文章分类
最新评论
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
MemCached的PHP客户端操作类一
<?php <br />//
//+---------------------------------------------------------------------------+
//|memcachedclient,PHP|
//+---------------------------------------------------------------------------+
//|Copyright(c)2003RyanT.Dean<rtdean>| <br>//|Allrightsreserved.| <br>//|| <br>//|Redistributionanduseinsourceandbinaryforms,withorwithout| <br>//|modification,arepermittedprovidedthatthefollowingconditions| <br>//|aremet:| <br>//|| <br>//|1.Redistributionsofsourcecodemustretaintheabovecopyright| <br>//|notice,thislistofconditionsandthefollowingdisclaimer.| <br>//|2.Redistributionsinbinaryformmustreproducetheabovecopyright| <br>//|notice,thislistofconditionsandthefollowingdisclaimerinthe| <br>//|documentationand/orothermaterialsprovidedwiththedistribution.| <br>//|| <br>//|THISSOFTWAREISPROVIDEDBYTHEAUTHOR``ASIS''ANDANYEXPRESSOR| <br>//|IMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIES| <br>//|OFMERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.| <br>//|INNOEVENTSHALLTHEAUTHORBELIABLEFORANYDIRECT,INDIRECT,| <br>//|INCIDENTAL,SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUT| <br>//|NOTLIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,| <br>//|DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY| <br>//|THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT| <br>//|(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOF| <br>//|THISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.| <br>//+---------------------------------------------------------------------------+ <br>//|Author:RyanT.Dean<rtdean>| <br>//|HeavilyinfluencedbythePerlmemcachedclientbyBradFitzpatrick.| <br>//|PermissiongrantedbyBradFitzpatrickforrelicenseofportedPerl| <br>//|clientlogicunder2-clauseBSDlicense.| <br>//+---------------------------------------------------------------------------+ <br>// <br>//$TCAnet$ <br>// <br><br>/** <br>*ThisisthePHPclientformemcached-adistributedmemorycachedaemon. <br>*Moreinformationisavailableathttp://www.danga.com/memcached/ <br>* <br>*Usageexample: <br>* <br>*require_once'memcached.php'; <br>* <br>*$mc=newmemcached(array( <br>*'servers'=>array('127.0.0.1:10000', <br>*array('192.0.0.1:10010',2), <br>*'127.0.0.1:10020'), <br>*'debug'=>false, <br>*'compress_threshold'=>10240, <br>*'persistant'=>true)); <br>* <br>*$mc->add('key',array('some','array')); <br>*$mc->replace('key','somerandomstring'); <br>*$val=$mc->get('key'); <br>* <br>*@authorRyanT.Dean<rtdean><br>*@packagememcached-client <br>*@version0.1.2 <br>*/ <br><br>//{{{requirements <br>//}}} <br><br>//{{{constants <br>//{{{flags <br><br>/** <br>*Flag:indicatesdataisserialized <br>*/ <br></rtdean></rtdean></rtdean>define("MEMCACHE_SERIALIZED",10);
/**
*Flag:indicatesdataiscompressed
*/
define("MEMCACHE_COMPRESSED",11);
//}}}
/**
*Minimumsavingstostoredatacompressed
*/
define("COMPRESSION_SAVINGS",0.20);
//}}}
//{{{classmemcached
/**
*memcachedclientclassimplementedusing(p)fsockopen()
*
*@authorRyanT.Dean<rtdean><br>*@packagememcached-client <br>*/ <br></rtdean>classmemcached
{
//{{{properties
//{{{public
/**
*Commandstatistics
*
*@vararray
*@accesspublic
*/
var$stats;
//}}}
//{{{private
/**
*CachedSocketsthatareconnected
*
*@vararray
*@accessprivate
*/
var$_cache_sock;
/**
*Currentdebugstatus;0-noneto9-profiling
*
*@varboolean
*@accessprivate
*/
var$_debug;
/**
*Deadhosts,assocarray,'host'=>'unixtimewhenoktocheckagain'
*
*@vararray
*@accessprivate
*/
var$_host_dead;
/**
*Iscompressionavailable?
*
*@varboolean
*@accessprivate
*/
var$_have_zlib;
/**
*Dowewanttousecompression?
*
*@varboolean
*@accessprivate
*/
var$_compress_enable;
/**
*Athowmanybytesshouldwecompress?
*
*@varinterger
*@accessprivate
*/
var$_compress_threshold;
/**
*Areweusingpersistantlinks?
*
*@varboolean
*@accessprivate
*/
var$_persistant;
/**
*Ifonlyusingoneserver;containsip:porttoconnectto
*
*@varstring
*@accessprivate
*/
var$_single_sock;
/**
*Arraycontainingip:portorarray(ip:port,weight)
*
*@vararray
*@accessprivate
*/
var$_servers;
/**
*Ourbitbuckets
*
*@vararray
*@accessprivate
*/
var$_buckets;
/**
*Total#ofbitbucketswehave
*
*@varinterger
*@accessprivate
*/
var$_bucketcount;
/**
*#oftotalserverswehave
*
*@varinterger
*@accessprivate
*/
var$_active;
//}}}
//}}}
//{{{methods
//{{{publicfunctions
//{{{memcached()
/**
*Memcacheinitializer
*
*@paramarray$argsAssociativearrayofsettings
*
*@returnmixed
*@accesspublic
*/
functionmemcached($args)
{
$this->set_servers($args['servers']);
$this->_debug=$args['debug'];
$this->stats=array();
$this->_compress_threshold=$args['compress_threshold'];
$this->_persistant=isset($args['persistant'])?$args['persistant']:false;
$this->_compress_enable=true;
$this->_have_zlib=function_exists("gzcompress");
$this->_cache_sock=array();
$this->_host_dead=array();
}
//}}}
//{{{add()
/**
*Addsakey/valuetothememcacheserverifoneisn'talreadysetwith
*thatkey
*
*@paramstring$keyKeytosetwithdata
*@parammixed$valValuetostore
*@paraminterger$exp(optional)Timetoexpiredataat
*
*@returnboolean
*@accesspublic
*/
functionadd($key,$val,$exp=0)
{
return$this->_set('add',$key,$val,$exp);
}
//}}}
//{{{decr()
/**
*Decrimentavaluestoredonthememcacheserver
*
*@paramstring$keyKeytodecriment
*@paraminterger$amt(optional)Amounttodecriment
*
*@returnmixedFALSEonfailure,valueonsuccess
*@accesspublic
*/
functiondecr($key,$amt=1)
{
return$this->_incrdecr('decr',$key,$amt);
}
//}}}
//{{{delete()
/**
*Deletesakeyfromtheserver,optionallyafter$time
*
*@paramstring$keyKeytodelete
*@paraminterger$time(optional)Howlongtowaitbeforedeleting
*
*@returnbooleanTRUEonsuccess,FALSEonfailure
*@accesspublic
*/
functiondelete($key,$time=0)
{
if(!$this->_active)
returnfalse;
$sock=$this->get_sock($key);
if(!is_resource($sock))
returnfalse;
$key=is_array($key)?$key[1]:$key;
$this->stats['delete']++;
$cmd="delete$key$time/r/n";
if(!fwrite($sock,$cmd,strlen($cmd)))
{
$this->_dead_sock($sock);
returnfalse;
}
$res=trim(fgets($sock));
if($this->_debug)
printf("MemCache:delete%s(%s)/n",$key,$res);
if($res=="DELETED")
returntrue;
returnfalse;
}
//}}}
//{{{disconnect_all()
/**
*Disconnectsallconnectedsockets
*
*@accesspublic
*/
functiondisconnect_all()
{
foreach($this->_cache_sockas$sock)
fclose($sock);
$this->_cache_sock=array();
}
//}}}
//{{{enable_compress()
/**
*Enable/Disablecompression
*
*@paramboolean$enableTRUEtoenable,FALSEtodisable
*
*@accesspublic
*/
functionenable_compress($enable)
{
$this->_compress_enable=$enable;
}
//}}}
//{{{forget_dead_hosts()
/**
*Forgetaboutallofthedeadhosts
*
*@accesspublic
*/
functionforget_dead_hosts()
{
$this->_host_dead=array();
}
//}}}
//{{{get()
/**
*Retrievesthevalueassociatedwiththekeyfromthememcacheserver
*
*@paramstring$keyKeytoretrieve
*
*@returnmixed
*@accesspublic
*/
functionget($key)
{
if(!$this->_active)
returnfalse;
$sock=$this->get_sock($key);
if(!is_resource($sock))
returnfalse;
$this->stats['get']++;
$cmd="get$key/r/n";
if(!fwrite($sock,$cmd,strlen($cmd)))
{
$this->_dead_sock($sock);
returnfalse;
}
$val=array();
$this->_load_items($sock,$val);
if($this->_debug)
foreach($valas$k=>$v)
printf("MemCache:sock%sgot%s=>%s/r/n",$sock,$k,$v);
return$val[$key];
}
//}}}
//{{{get_multi()
/**
*Getmultiplekeysfromtheserver(s)
*
*@paramarray$keysKeystoretrieve
*
*@returnarray
*@accesspublic
*/
functionget_multi($keys)
{
if(!$this->_active)
returnfalse;
$this->stats['get_multi']++;
foreach($keysas$key)
{
$sock=$this->get_sock($key);
if(!is_resource($sock))continue;
$key=is_array($key)?$key[1]:$key;
if(!isset($sock_keys[$sock]))
{
$sock_keys[$sock]=array();
$socks[]=$sock;
}
$sock_keys[$sock][]=$key;
}
//Sendouttherequests
foreach($socksas$sock)
{
$cmd="get";
foreach($sock_keys[$sock]as$key)
{
$cmd.="".$key;
}
$cmd.="/r/n";
if(fwrite($sock,$cmd,strlen($cmd)))
{
$gather[]=$sock;
}else
{
$this->_dead_sock($sock);
}
}
//Parseresponses
$val=array();
foreach($gatheras$sock)
{
$this->_load_items($sock,$val);
}
if($this->_debug)
foreach($valas$k=>$v)
printf("MemCache:got%s=>%s/r/n",$k,$v);
return$val;
}
//}}}
//{{{incr()
/**
*Increments$key(optionally)by$amt
*
*@paramstring$keyKeytoincrement
*@paraminterger$amt(optional)amounttoincrement
*
*@returnintergerNewkeyvalue?
*@accesspublic
*/
functionincr($key,$amt=1)
{
return$this->_incrdecr('incr',$key,$amt);
}
//}}}
//{{{replace()
/**
*Overwritesanexistingvalueforkey;onlyworksifkeyisalreadyset
*
*@paramstring$keyKeytosetvalueas
*@parammixed$valueValuetostore
*@paraminterger$exp(optional)Experiationtime
*
*@returnboolean
*@accesspublic
*/
functionreplace($key,$value,$exp=0)
{
return$this->_set('replace',$key,$value,$exp);
}
//}}}
//{{{run_command()
/**
*Passesthrough$cmdtothememcacheserverconnectedby$sock;returns
*outputasanarray(nullarrayifnooutput)
*
*NOTE:duetoapossiblebuginhowPHPreadswhileusingfgets(),each
*linemaynotbeterminatedbya/r/n.Morespecifically,mytesting
*hasshownthat,onFreeBSDatleast,eachlineisterminatedonly
*witha/n.ThisiswiththePHPflagauto_detect_line_endingsset
*tofalase(thedefault).
*
*@paramresource$sockSockettosendcommandon
*@paramstring$cmdCommandtorun
*
*@returnarrayOutputarray
*@accesspublic
*/
functionrun_command($sock,$cmd)
{
if(!is_resource($sock))
returnarray();
if(!fwrite($sock,$cmd,strlen($cmd)))
returnarray();
while(true)
{
$res=fgets($sock);
$ret[]=$res;
if(preg_match('/^END/',$res))
break;
if(strlen($res)==0)
break;
}
return$ret;
}
//}}}
//{{{set()
/**
*Unconditionallysetsakeytoagivenvalueinthememcache.Returnstrue
*ifsetsuccessfully.
*
*@paramstring$keyKeytosetvalueas
*@parammixed$valueValuetoset
*@paraminterger$exp(optional)Experiationtime
*
*@returnbooleanTRUEonsuccess
*@accesspublic
*/
functionset($key,$value,$exp=0)
{
return$this->_set('set',$key,$value,$exp);
}
//}}}
//{{{set_compress_threshold()
/**
*Setsthecompressionthreshold
*
*@paraminterger$threshThresholdtocompressiflargerthan
*
*@accesspublic
*/
functionset_compress_threshold($thresh)
{
$this->_compress_threshold=$thresh;
}
//}}}
//{{{set_debug()
/**
*Setsthedebugflag
*
*@paramboolean$dbgTRUEfordebugging,FALSEotherwise
*
*@accesspublic
*
*@seememcahced::memcached
*/
functionset_debug($dbg)
{
$this->_debug=$dbg;
}
//}}}
//{{{set_servers()
/**
*Setstheserverlisttodistributekeygetsandputsbetween
*
*@paramarray$listArrayofserverstoconnectto
*
*@accesspublic
*
*@seememcached::memcached()
*/
functionset_servers($list)
{
$this->_servers=$list;
$this->_active=count($list);
$this->_buckets=null;
$this->_bucketcount=0;
$this->_single_sock=null;
if($this->_active==1)
$this->_single_sock=$this->_servers[0];
}
//}}}
//}}}
//{{{privatemethods
//{{{_close_sock()
/**
*Closethespecifiedsocket
*
*@paramstring$sockSockettoclose
*
*@accessprivate
*/
function_close_sock($sock)
{
$host=array_search($sock,$this->_cache_sock);
fclose($this->_cache_sock[$host]);
unset($this->_cache_sock[$host]);
}
//}}}
//{{{_connect_sock()
/**
*Connects$sockto$host,timingoutafter$timeout
*
*@paraminterger$sockSockettoconnect
*@paramstring$hostHost:IPtoconnectto
*@paramfloat$timeout(optional)Timeoutvalue,defaultsto0.25s
*
*@returnboolean
*@accessprivate
*/
function_connect_sock(&$sock,$host,$timeout=0.25)
{
list($ip,$port)=explode(":",$host);
if($this->_persistant==1)
{
$sock=@pfsockopen($ip,$port,$errno,$errstr,$timeout);
}else
{
$sock=@fsockopen($ip,$port,$errno,$errstr,$timeout);
}
if(!$sock)
returnfalse;
returntrue;
}
//}}}
//{{{_dead_sock()
/**
*Marksahostasdeaduntil30-40secondsinthefuture
*
*@paramstring$sockSockettomarkasdead
*
*@accessprivate
*/
function_dead_sock($sock)
{
$host=array_search($sock,$this->_cache_sock);
list($ip,$port)=explode(":",$host);
$this->_host_dead[$ip]=time()+30+intval(rand(0,10));
$this->_host_dead[$host]=$this->_host_dead[$ip];
unset($this->_cache_sock[$host]);
}
//}}}
//{{{get_sock()
/**
*get_sock
*
*@paramstring$keyKeytoretrievevaluefor;
*
*@returnmixedresourceonsuccess,falseonfailure
*@accessprivate
*/
functionget_sock($key)
{
if(!$this->_active)
returnfalse;
if($this->_single_sock!==null)
return$this->sock_to_host($this->_single_sock);
$hv=is_array($key)?intval($key[0]):$this->_hashfunc($key);
if($this->_buckets===null)
{
foreach($this->_serversas$v)
{
if(is_array($v))
{
for($i=0;$i$v[1];$i++)
$bu[]=$v[0];
}else
{
$bu[]=$v;
}
}
$this->_buckets=$bu;
$this->_bucketcount=count($bu);
}
$realkey=is_array($key)?$key[1]:$key;
for($tries=0;$tries20;$tries++)
{
$host=$this->_buckets[$hv%$this->_bucketcount];
$sock=$this->sock_to_host($host);
if(is_resource($sock))
return$sock;
$hv+=$this->_hashfunc($tries.$realkey);
}
returnfalse;
}
//}}}
//{{{_hashfunc()
/**
*Createsahashintergerbasedonthe$key
*
*@paramstring$keyKeytohash
*
*@returnintergerHashvalue
*@accessprivate
*/
function_hashfunc($key)
{
$hash=0;
for($i=0;$istrlen($key);$i++)
{
$hash=$hash*33+ord($key[$i]);
}
return$hash;
}
//}}}
//{{{_incrdecr()
/**
*Performincrement/decrimenton$key
*
*@paramstring$cmdCommandtoperform
*@paramstring$keyKeytoperformiton
*@paraminterger$amtAmounttoadjust
*
*@returnintergerNewvalueof$key
*@accessprivate
*/
function_incrdecr($cmd,$key,$amt=1)
{
if(!$this->_active)
returnnull;
$sock=$this->get_sock($key);
if(!is_resource($sock))
returnnull;
$key=is_array($key)?$key[1]:$key;
$this->stats[$cmd]++;
if(!fwrite($sock,"$cmd$key$amt/r/n"))
return$this->_dead_sock($sock);
stream_set_timeout($sock,1,0);
$line=fgets($sock);
if(!preg_match('/^(/d+)/',$line,$match))
returnnull;
return$match[1];
}
//}}}
//{{{_load_items()
/**
*Loaditemsinto$retfrom$sock
*
*@paramresource$sockSockettoreadfrom
*@paramarray$retReturnedvalues
*
*@accessprivate
*/
function_load_items($sock,&$ret)
{
while(1)
{
$decl=fgets($sock);
if($decl=="END/r/n")
{
returntrue;
}elseif(preg_match('/^VALUE(/S+)(/d+)(/d+)/r/n$/',$decl,$match))
{
list($rkey,$flags,$len)=array($match[1],$match[2],$match[3]);
$bneed=$len+2;
$offset=0;
while($bneed>0)
{
$data=fread($sock,$bneed);
$n=strlen($data);
if($n==0)
break;
$offset+=$n;
$bneed-=$n;
$ret[$rkey].=$data;
}
if($offset!=$len+2)
{
//Somethingisborked!
if($this->_debug)
printf("Somethingisborked!key%sexpecting%dgot%dlength/n",$rkey,$len+2,$offset);
unset($ret[$rkey]);
$this->_close_sock($sock);
returnfalse;
}
$ret[$rkey]=rtrim($ret[$rkey]);
if($this->_have_zlib&&$flags&MEMCACHE_COMPRESSED)
$ret[$rkey]=gzuncompress($ret[$rkey]);
if($flags&MEMCACHE_SERIALIZED)
$ret[$rkey]=unserialize($ret[$rkey]);
}else
{
if($this->_debug)
print("Errorparsingmemcachedresponse/n");
return0;
}
}
}
//}}}
//{{{_set()
/**
*Performstherequestedstorageoperationtothememcacheserver
*
*@paramstring$cmdCommandtoperform
*@paramstring$keyKeytoacton
*@parammixed$valWhatweneedtostore
*@paraminterger$expWhenitshouldexpire
*
*@returnboolean
*@accessprivate
*/
function_set($cmd,$key,$val,$exp)
{
if(!$this->_active)
returnfalse;
$sock=$this->get_sock($key);
if(!is_resource($sock))
returnfalse;
$this->stats[$cmd]++;
$flags=0;
if(!is_scalar($val))
{
$val=serialize($val);
$flags|=MEMCACHE_SERIALIZED;
if($this->_debug)
printf("client:serializingdataasitisnotscalar/n");
}
$len=strlen($val);
if($this->_have_zlib&&$this->_compress_enable&&
$this->_compress_threshold&&$len>=$this->_compress_threshold)
{
$c_val=gzcompress($val,9);
$c_len=strlen($c_val);
if($c_len$len*(1-COMPRESS_SAVINGS))
{
if($this->_debug)
printf("client:compressingdata;was%dbytesisnow%dbytes/n",$len,$c_len);
$val=$c_val;
$len=$c_len;
$flags|=MEMCACHE_COMPRESSED;
}
}
if(!fwrite($sock,"$cmd$key$flags$exp$len/r/n$val/r/n"))
return$this->_dead_sock($sock);
$line=trim(fgets($sock));
if($this->_debug)
{
if($flags&MEMCACHE_COMPRESSED)
$val='compresseddata';
printf("MemCache:%s%s=>%s(%s)/n",$cmd,$key,$val,$line);
}
if($line=="STORED")
returntrue;
returnfalse;
}
//}}}
//{{{sock_to_host()
/**
*Returnsthesocketforthehost
*
*@paramstring$hostHost:IPtogetsocketfor
*
*@returnmixedIOStreamorfalse
*@accessprivate
*/
functionsock_to_host($host)
{
if(isset($this->_cache_sock[$host]))
return$this->_cache_sock[$host];
$now=time();
list($ip,$port)=explode(":",$host);
if(isset($this->_host_dead[$host])&&$this->_host_dead[$host]>$now||
isset($this->_host_dead[$ip])&&$this->_host_dead[$ip]>$now)
returnnull;
if(!$this->_connect_sock($sock,$host))
return$this->_dead_sock($host);
//Donotbufferwrites
stream_set_write_buffer($sock,0);
$this->_cache_sock[$host]=$sock;
return$this->_cache_sock[$host];
}
//}}}
//}}}
//}}}
}
//}}}
?>
发表评论
-
规范的模板化项目架构管理
2012-05-03 23:31 848总在寻找项目开发简单化、标准化、统一化的开发管理方法,在项目 ... -
Java的ftp上传下载工具
2012-04-05 22:17 1720自己写的利用apache的net包写的ftp的上传、下 ... -
SmartGWT学习注意事项(一)
2012-03-31 20:42 1535首先表明,我 ... -
J2ME程序开发全方位基础讲解汇总
2007-08-03 14:19 620一、J2ME中需要的Java基础知识现在有大部分人,都是从零开 ... -
使用J2ME技术开发RPG游戏
2007-08-03 14:24 550RPG(角色扮演游戏)是手机游戏中的一类主要类型,也是相对来说 ... -
移动视频: QuickTime for Java API 入门
2007-08-05 12:45 620在 Java 平台上创建 iPod 视频内容 ... -
技术交流:QuickTime流媒体和Java(图)
2007-08-05 12:46 631这并不是即将问世的Quic ... -
JMF下载安装与支持格式
2007-08-05 12:47 853JMF开发进度不是很快,所以目前还是比较薄弱。 JMF,全名 ... -
搭建J2ME开发环境
2007-08-05 12:48 566由于WTK并没有提供代码编辑的功能,因此本文讲述如何使用Ecl ... -
JMF系统介绍
2007-08-09 15:18 699一.简介 1.1JMF 体系结构 ... -
Experiments in Streaming Content in Java ME(一)
2007-08-13 13:43 578Since my book on Mobile Media A ... -
Experiments in Streaming Content in Java ME(二)----Creating an RTSP Protocol Handler
2007-08-13 13:44 1446Recall that RTSP is the actual ... -
Experiments in Streaming Content in Java ME(三)-----Back to RTPSourceStream and StreamingDataSource
2007-08-13 13:46 1029With the protocol handler in pl ... -
java中文件操作大全
2007-08-22 15:40 554一.获得控制台用户输入的信息 /***//**获得控 ... -
Pocket PC、Pocket PC Phone、Smartphone的区别
2007-08-23 16:59 653首先说明几个概念: 1、什么是Pocket PC?Pocket ... -
Struts配置文件详解(来自csdn)
2007-11-30 08:35 497Struts应用采用两个基于X ... -
jBPM开发入门指南(5)
2007-11-30 08:45 510jBPM开发入门指南(5) 前篇说起要讲在JBPM中实现用 ... -
jBPM开发入门指南(4)
2007-11-30 08:48 587jBPM开发入门指南(4) 7 jBPM 的客户端 ... -
jBPM开发入门指南(3)
2007-11-30 08:51 6135 安装 jBPM 的 Eclipse 开发插件 有 ... -
jBPM开发入门指南(2)
2007-11-30 08:52 6234 数据库初始化 jBPM 需要数据库支持, jBPM ...
相关推荐
### HashMap与HashTable和HashSet的区别 #### 一、概述 在Java集合框架中,`HashMap`, `HashTable` 和 `HashSet` 是三个重要的数据结构,它们分别实现了`Map`接口和`Set`接口,提供了不同的功能来满足不同的编程...
Java集合专题总结:HashMap和HashTable源码...本文总结了HashMap和HashTable的源码学习和面试总结,涵盖了它们的存储结构、构造方法、get和put方法的源码分析、Hash表的特点和缺点、HashTable和HashMap的区别等内容。
总的来说,HashMap、HashTable和HashSet各有其特点和适用场景。HashMap适合单线程环境和对性能要求高的场合,HashTable适用于需要线程安全但对性能要求不那么高的情况,而HashSet则是存储不重复元素的高效选择。了解...
在Java编程语言中,`HashMap`、`Hashtable`和`HashSet`都是集合框架的重要组成部分,分别用于存储键值对和不重复元素。下面将详细解释它们之间的区别。 首先,`Hashtable`是`Map`接口的一个早期实现,它提供了一个...
`HashMap`、`Hashtable`和`HashSet`都是基于`Map`或`Set`接口实现的不同数据结构,它们在功能、线程安全性和性能等方面有显著差异。 首先,`HashMap`和`Hashtable`都实现了`Map`接口,这意味着它们都可以存储键值对...
### Hashtable与HashMap的区别详解 #### 一、基本概念与历史背景 在Java编程语言中,`Hashtable` 和 `HashMap` 都是用来存储键值对的数据结构。这两种数据结构虽然相似,但是在实现细节上存在显著差异。 1. **...
HashSet实现了Set接口,它不允许集合中有重复的值,当我们提到HashSet时,第一件事情就是在将对象存储在HashSet之前,要先确保对象重写equals()和hashCode()方法,这样才能比较对象的值是否相等,以确保set中没有...
### Java中HashMap, LinkedHashMap, TreeMap,HashTable的区别 在Java编程语言中,`Map`接口是集合框架中的一个重要组成部分,用于存储键值对。本文将详细分析四种常用的`Map`实现类:`HashMap`, `LinkedHashMap`, ...
4. HashMap与Hashtable的区别? 答:HashMap非线程安全,而Hashtable是线程安全的;HashMap允许null键值,Hashtable不允;HashMap迭代器在修改时不会抛出ConcurrentModificationException,而Hashtable会。 5. ...
ArrayList和LinkedList虽然不是Set,但它们的父接口List属于Collection,而Collection接口有一个子接口Set,例如HashSet是Set接口的一个实现,它内部基于HashMap实现,保证元素唯一性。 7. WeakHashMap WeakHashMap...
HashMap和HashTable是Java中两种常用的基于哈希表的数据结构,它们的主要区别如下: 1. **父类不同**:HashMap继承自AbstractMap类,而HashTable继承自Dictionary类。AbstractMap是Map接口的一个抽象实现,提供了...
HashMap和HashTable的主要区别是HashMap线程不安全,而HashTable线程安全。 HashMap允许键值为null,而HashTable不允许键值为null。HashMap继承自AbstractMap,而HashTable继承自Dictionary。 在选择HashMap还是...
- **HashMap和HashTable的区别**:列举HashMap和HashTable的主要区别。 - **HashMap和HashSet的区别**:解释HashMap和HashSet之间的区别。 - **扩容机制**:HashMap是如何进行扩容的? - **长度限制**:解释为什么...
本文将深入探讨Java中的常用集合框架,包括Map、Set和List接口以及它们的一些实现类,如HashMap、Hashtable、HashSet、ArrayList、LinkedList和Vector。 首先,我们来看Map接口。Map接口定义了一个键值对的存储结构...
本文主要探讨了几个关键的集合接口和实现类的底层源码,包括List、HashMap、HashSet等,以及它们的基本操作。 首先,Collection接口是所有单值集合的父接口,提供了增加、删除、遍历元素的基本方法。例如,`add()`...
#### 八、HashMap与Hashtable、HashSet、TreeMap的区别 - **HashMap与Hashtable**: - `HashMap`非线程安全,而`Hashtable`线程安全。 - `HashMap`允许键和值为`null`,而`Hashtable`不允许。 - **HashMap与...
7. **HashMap与Hashtable、HashSet、TreeMap的区别**: - HashMap与Hashtable:HashMap非线程安全,而Hashtable是线程安全的,但性能较低,不推荐在现代Java中使用。 - HashMap与HashSet:HashMap存储键值对,...
java jdk源码学习 JavaSourceLearn 版本号 版本 corretto-1.8.0_275 方式 逐步阅读源码添加注释、notes文件夹添加笔记 计划学习任务计划 标题为包名,后面序号为优先级1-4,...Hashtable 1 HashSet 1 LinkedHashMa
本资源摘要信息涵盖了 Java 集合框架的基本概念和实现细节,着重介绍了 Java 集合框架中的 HashMap、Hashtable、ArrayList、LinkedList 等常用类,并对比了 Hashtable 和 HashMap 的区别,详细分析了两者在源代码...