收藏列表
- 全部 [10]
- java [3]
- php [1]
- volatile [1]
- mongo [1]
- mongodb java [1]
- mongodb collection [1]
- mongocursor mongodb [1]
- mongodb php 例子 [1]
标题 | 标签 | 来源 | |
volatile | volatile | java CAS原语 | |
volatile类型: 根据Java Language Specification中的说明, jvm系统中存在一个主内存(Main Memory或Java Heap Memory),Java中所有变量都储存在主存中,对于所有线程都是共享的。 每条线程都有自己的工作内存(Working Memory),工作内存中保存的是主存中某些变量的拷贝,线程对所有变量的操作都是在工作内存中进行,线程之间无法相互直接访问,变量传递均需要通过主存完成。 所以,同一变量的值在工作内存和主存中可能不一致。volatile其实是告诉处理器, 不要将我放入工作内存, 请直接在主存操作我。 |
|||
搜狐直播代码 | php | http://bbs.cenfun.com/thread-17700-1-1.html | |
<?php function curl($url, $bool) { $user_agent = $_SERVER['HTTP_USER_AGENT']; $ch = curl_init(); $timeout = 30; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $data = curl_exec($ch); curl_close($ch); if ($bool) { return iconv('gbk', 'utf-8', $data); } return $data; } function tv_list() { global $thisurl; $url = "http://live.tv.sohu.com/"; $url = curl($url,0); preg_match('|var data1 = \{\"data\":\[([^;]+)\]\};|',$url,$ar); preg_match_all('/"tvId":(.*?),"tvPathName":/',$ar[1], $arr); preg_match_all('/.gif","name":"(.*?)","url":/i',$ar[1], $name); $arr=$arr[1]; $name=$name[1]; $list = "<list>\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=4" label="CNN新闻" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=11" label="CCTV-4" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=1" label="CCTV-13" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=2" label="凤凰资讯" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=46" label="CCTV-1HD" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=47" label="东森亚洲" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=48" label="香港卫视" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=49" label="东森新闻" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=70" label="大片影院" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=71" label="CCTV-6" />'."\n"; $list .= '<m type="2" src="http://gslb.tv.sohu.com/live?cid=100" label="未知台" />'."\n"; foreach ($arr as $k => $v) { $list .= "<m type=\"2\" src=\"$thisurl?vid=$v\" label=\"$name[$k]\" />\n"; } return "$list</list>"; } function tv_vid($vid) { $url="http://live.tv.sohu.com/live/player_json.jhtml?lid=$vid&type=1"; $url = curl($url,0); preg_match('|\["([^]]+)"\]|',$url,$ar); $ar = $ar[1]; header("location:http://$ar"); } $thisurl = "http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF']; $xml=""; if(isset($_GET['vid'])){ $xml.=tv_vid($_GET['vid']); }else{ $xml.=tv_list(); } echo $xml; ?> |
|||
Java MD5加密 | java | Java MD5加密 | |
/** * MD5 加密 */ private String getMD5Str(String str) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(str.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException caught!"); System.exit(-1); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.toString(); } |
|||
从命令窗口接收字符串 | java | ||
package client; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; public class MessageClientHandler extends SimpleChannelUpstreamHandler { private static final Logger logger = Logger .getLogger(MessageClientHandler.class.getName()); @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { //测试用 Scanner reader = new Scanner(System.in); System.out.println("正在连接,请输入JSON格式的字符串"); System.out.println("例如:{'action':'Test.login','arg1':'argval'}"); String message = reader.next(); e.getChannel().write(message); } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { // Send back the received message to the remote peer. System.err.println("messageReceived send message " + e.getMessage()); try { Thread.sleep(1000 * 3); } catch (Exception ex) { ex.printStackTrace(); } //测试用 Scanner reader = new Scanner(System.in); System.out.println("请输入JSON格式的字符串:"); System.out.println("例如:{'action':'Test.login','arg1':'argval'}"); String message = reader.next(); e.getChannel().write(message); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { // Close the connection when an exception is raised. logger.log(Level.WARNING, "Unexpected exception from downstream.", e.getCause()); e.getChannel().close(); } } |
|||
java 单例模式例子 | java | ||
/** * Created by IntelliJ IDEA. * User: yiminghe * Date: 2009-6-8 * Time: 19:20:52 */ public class Singleton { public static void main(String[] args) throws Exception{ System.out.println(Class.forName("S1")); System.out.println(Class.forName("S2")); System.out.println(Class.forName("S3")); System.out.println(Class.forName("S4")); } } /* 预先加载法 优点:1.线程安全的, 2.在类加载的同时已经创建好一个静态对象,调用时反应速度快。 缺点: 资源利用效率不高,可能这个单例不会需要使用也被系统加载 */ class S1 { private S1() { System.out.println("ok1"); } private static S1 instance = new S1(); public static S1 getInstance() { return instance; } } /* initialization on demand,延迟加载法 (考虑多线程) 优点:1.资源利用率高 缺点:第一次加载是发应不快 ,多线程使用不必要的同步开销大 */ class S2 { private S2() { System.out.println("ok2"); } private static S2 instance = null; public static synchronized S2 getInstance() { if (instance == null) instance = new S2(); return instance; } } /* initialization on demand - double check 延迟加载法改进之双重检测 (考虑多线程) 优点:1.资源利用率高 缺点:第一次加载是发应不快 ,由于java 内存模型一些原因偶尔会失败 */ class S3 { private S3() { System.out.println("ok3"); } private static S3 instance = null; public static S3 getInstance() { if (instance == null) { synchronized (S3.class) { if (instance == null) instance = new S3(); } } return instance; } } /* initialization on demand holder (考虑多线程) 优点:1.资源利用率高 缺点:第一次加载是发应不快 */ class S4 { private S4() { System.out.println("ok4"); } private static class S4Holder { static S4 instance = new S4(); } public static S4 getInstance() { return S4Holder.instance; } } |
|||
Mongodb php 例子 | mongodb php 例子 | ||
直接实例化mongo类+创建连接: $mo = new Mongo();//得到一个Mongo连接对象 实例化了一个Mongo类,并且与默认的localhost:27017端口的mongoDB建立连接。 如果想连接到其他的主机,可以这样写:$mongo = new Mongo("mongodb://username:password@192.168.1.22:12345"); 另外一种方式,实例化mongo类,再手动建立连接: $mongo = new Mongo("mongodb://username:password@192.168.1.22:12345",array('connect'=>false));//初始化类 $mongo->connect();//创建连接 Mongo类中有用的一些方法: Mongo::listDBs() http://us.php.net/manual/en/mongo.listdbs.php 返回一个包含当前mongo服务上的库(DB)信息的数组。 $mo = new Mongo(); $dbs = $mo->listDBs();//获得一个包含db信息的数组 Mongo::selectCollection($db,$coll) http://us.php.net/manual/en/mongo.selectcollection.php 返回一个当前连接下的某db中的collection对象。 $mo = new Mongo(); $coll = $mo->selectCollection(’db’,'mycoll’);//得到一个collection对象 选择想要的数据库(Mongo类): 一种方式: http://us.php.net/manual/en/mongo.get.php $mongo = new Mongo(); $db = $mongo->foo;//得到一个MongoDB对象 另一种方式: http://us.php.net/manual/en/mongo.selectdb.php $mongo = new Mongo(); $db = $mongo->selectDB(’foo’);//得到一个MongoDB对象 MongoDB中有用的函数: 创建一个MongoDB对象 http://us.php.net/manual/en/mongodb.construct.php $mo = new Mongo(); $db = new MongoDB($mo,’dbname’);//通过创建方式获得一个MongoDB对象 删除当前DB http://us.php.net/manual/en/mongodb.drop.php $db = $mo->dbname; $db->drop(); 获得当前数据库名 http://us.php.net/manual/en/mongodb.–tostring.php $db = $mo->dbname; $db->_tostring(); 选择想要的collection: A: $mo = new Mongo(); $coll = $mo->dbname->collname;//获得一个collection对象 B: $db = $mo->selectDB(’dbname’); $coll = $db->collname; C: $db = $mo->dbname; $coll = $db->selectCollectoin(’collname’);//获得一个collection对象 插入数据(MongoCollection对象): http://us.php.net/manual/en/mongocollection.insert.php MongoCollection::insert(array $a,array $options) array $a 要插入的数组 array $options 选项 safe 是否返回操作结果信息 fsync 是否直接插入到物理硬盘 例程: $coll = $mo->db->foo; $a = array(’a'=>’b'); $options = array(’safe’=>true); $rs =$coll->insert($a,$options); $rs为一个array型的数组,包含操作信息 删除数据库中的记录(MongoCollection对象): http://us.php.net/manual/en/mongocollection.remove.php MongoCollection::remove(array $criteria,array $options) array $criteria 条件 array $options 选项 safe 是否返回操作结果 fsync 是否是直接影响到物理硬盘 justOne 是否只影响一条记录 例程: $coll = $mo->db->coll; $c = array(’a'=>1,’s’=>array(’$lt’=>100)); $options = array(’safe’=>true); $rs = $coll->remove($c,$options); $rs为一个array型的数组,包含操作信息 更新数据库中的记录(MongoCollection对象): http://us.php.net/manual/en/mongocollection.update.php MongoCollection::update(array $criceria,array $newobj,array $options) array $criteria 条件 array $newobj 要更新的内容 array $options 选项 safe 是否返回操作结果 fsync 是否是直接影响到物理硬盘 upsert 是否没有匹配数据就添加一条新的 multiple 是否影响所有符合条件的记录,默认只影响一条 例程: $coll = $mo->db->coll; $c = array(’a'=>1,’s’=>array(’$lt’=>100)); $newobj = array(’e'=>’f',’x'=>’y'); $options = array(’safe’=>true,’multiple’=>true); $rs = $coll->remove($c,$newobj,$options); $rs为一个array型的数组,包含操作信息 查询collection获得单条记录(MongoCollection类): http://us.php.net/manual/en/mongocollection.findone.php array MongoCollection::findOne(array $query,array $fields) array $query 条件 array $fields 要获得的字段 例程: $coll = $mo->db->coll; $query = array(’s’=>array(’$lt’=>100)); $fields = array(’a'=>true,’b'=>true); $rs = $coll->findOne($query,$fields); 如果有结果就返回一个array,如果没有结果就返回NULL 查询collection获得多条记录(MongoCollection类): http://us.php.net/manual/en/mongocollection.find.php MongoCursor MongoCollection::find(array $query,array $fields) array $query 条件 array $fields 要获得的字段 例程: $coll = $mo->db->coll; $query = array(’s’=>array(’$lt’=>100)); $fields = array(’a'=>true,’b'=>true); $cursor = $coll->find($query,$fields); 返回一个游标记录对象MongoCursor。 针对游标对象MongoCursor的操作(MongoCursor类): http://us.php.net/manual/en/class.mongocursor.php 循环或的结果记录: $cursor = $coll->find($query,$fields); while($cursor->hasNext()){ $r = $cursor->getNext(); var_dump($r); } 或者 $cursor = $coll->find($query,$fields); foreache($cursor as $k=>$v){ var_dump($v); } 或者 $cursor = $coll->find($query,$fields); $array= iterator_to_array($cursor); 一个血的教训: http://us.php.net/manual/en/mongocursor.snapshot.php 详见 不断变化的MongDB结果集 在我们做了find()操作,获得$cursor游标之后,这个游标还是动态的,也就是 在我获得游标到我循环操作完成对应记录的过程中,默认情况下,这对符合条件的记录如果增加,结果集也会自动增加。换句话说,在我find()之后,到我的 游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$cursor获得。 如果你想在获得$cursor之后的结果集不变化,需要这样做: $cursor = $coll->find($query,$fields); $cursor->snapshot(); foreache($cursor as $k=>$v){ var_dump($v); } |
|||
针对查询结果集的操作:MongoCursor | mongocursor mongodb | ||
implements Iterator { /* Static Fields */ static boolean $MongoCursor->slaveOkay = FALSE ; static integer $timeout = 20000 ; /* Methods */ public MongoCursor MongoCursor::addOption ( string $key , mixed $value ) public MongoCursor MongoCursor::batchSize ( int $num ) MongoCursor::__construct ( Mongo $connection , string $ns [, array $query = array() [, array $fields = array() ]] ) public int MongoCursor::count ([ bool $foundOnly = FALSE ] ) public array MongoCursor::current ( void ) public bool MongoCursor::dead ( void ) protected void MongoCursor::doQuery ( void ) public array MongoCursor::explain ( void ) public MongoCursor MongoCursor::fields ( array $f ) public array MongoCursor::getNext ( void ) public bool MongoCursor::hasNext ( void ) public MongoCursor MongoCursor::hint ( array $key_pattern ) public MongoCursor MongoCursor::immortal ([ bool $liveForever = true ] ) public array MongoCursor::info ( void ) public string MongoCursor::key ( void ) public MongoCursor MongoCursor::limit ( int $num ) public void MongoCursor::next ( void ) public MongoCursor MongoCursor::partial ([ bool $okay = true ] ) public void MongoCursor::reset ( void ) public void MongoCursor::rewind ( void ) public MongoCursor MongoCursor::skip ( int $num ) public MongoCursor MongoCursor::slaveOkay ([ bool $okay = true ] ) public MongoCursor MongoCursor::snapshot ( void ) public MongoCursor MongoCursor::sort ( array $fields ) public MongoCursor MongoCursor::tailable ([ bool $tail = true ] ) public MongoCursor MongoCursor::timeout ( int $ms ) public bool MongoCursor::valid ( void ) } //使用方法 $m = new Mongo(); // connect $db = $m->selectDB("example"); //$cursor = $collection->find(); //var_dump(iterator_to_array($cursor)); $cursor = $collection->find(); foreach ($cursor as $doc) { // do something to each document } |
|||
针对mongoDB中collection的操作:MongoCollection | mongodb collection | ||
MongoCollection { /* Constants */ const int ASCENDING = 1 ; const int DESCENDING = -1 ; /* Fields */ public MongoDB $MongoCollection->db = NULL ; public integer $w ; public integer $wtimeout ; /* Methods */ public mixed MongoCollection::batchInsert ( array $a [, array $options = array() ] ) public MongoCollection::__construct ( MongoDB $db , string $name ) public int MongoCollection::count ([ array $query = array() [, int $limit = 0 [, int $skip = 0 ]]] ) public array MongoCollection::createDBRef ( array $a ) public array MongoCollection::deleteIndex ( string|array $keys ) public array MongoCollection::deleteIndexes ( void ) public array MongoCollection::drop ( void ) public bool MongoCollection::ensureIndex ( array $keys [, array $options = array() ] ) public MongoCursor MongoCollection::find ([ array $query = array() [, array $fields = array() ]] ) public array MongoCollection::findOne ([ array $query = array() [, array $fields = array() ]] ) public MongoCollection MongoCollection::__get ( string $name ) public array MongoCollection::getDBRef ( array $ref ) public array MongoCollection::getIndexInfo ( void ) public string MongoCollection::getName ( void ) public bool MongoCollection::getSlaveOkay ( void ) public array MongoCollection::group ( mixed $keys , array $initial , MongoCode $reduce [, array $options = array() ] ) public mixed MongoCollection::insert ( array $a [, array $options = array() ] ) public mixed MongoCollection::remove ([ array $criteria = array() [, array $options = array() ]] ) public mixed MongoCollection::save ( array $a [, array $options = array() ] ) public bool MongoCollection::setSlaveOkay ([ bool $ok = true ] ) static protected string MongoCollection::toIndexString ( mixed $keys ) public string MongoCollection::__toString ( void ) public bool MongoCollection::update ( array $criteria , array $newobj [, array $options = array() ] ) public array MongoCollection::validate ([ bool $scan_data = FALSE ] ) } |
|||
针对mongoDB连接的操作:Mongo | mongo | ||
Mongo { /* Constants */ const string VERSION ; const string DEFAULT_HOST = "localhost" ; const int DEFAULT_PORT = 27017 ; /* Fields */ public boolean $Mongo->connected = FALSE ; public string $status = NULL ; protected string $server = NULL ; protected boolean $persistent = NULL ; /* Methods */ public bool Mongo::close ( void ) public bool Mongo::connect ( void ) protected bool Mongo::connectUtil ( void ) Mongo::__construct ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] ) public array Mongo::dropDB ( mixed $db ) public MongoDB Mongo::__get ( string $dbname ) public array Mongo::getHosts ( void ) public static int Mongo::getPoolSize ( void ) public string Mongo::getSlave ( void ) public bool Mongo::getSlaveOkay ( void ) public array Mongo::listDBs ( void ) public array Mongo::poolDebug ( void ) public MongoCollection Mongo::selectCollection ( string $db , string $collection ) public MongoDB Mongo::selectDB ( string $name ) public static bool Mongo::setPoolSize ( int $size ) public bool Mongo::setSlaveOkay ([ bool $ok = true ] ) public string Mongo::switchSlave ( void ) public string Mongo::__toString ( void ) } //使用的方法一 $m = new Mongo(); // connect $db = $m->foo; // get the database named "foo" //使用的方法二 function MongoConnect($username, $password, $database, $host) { $con = new Mongo("mongodb://{$username}:{$password}@{$host}"); // Connect to Mongo Server $db = $con->selectDB($database); // Connect to Database } MongoDB { /* Constants */ const int PROFILING_OFF = 0 ; const int PROFILING_SLOW = 1 ; const int PROFILING_ON = 2 ; /* Fields */ public integer $MongoDB->w = 1 ; public integer $wtimeout = 10000 ; /* Methods */ public array MongoDB::authenticate ( string $username , string $password ) public array MongoDB::command ( array $command [, array $options = array() ] ) MongoDB::__construct ( Mongo $conn , string $name ) public MongoCollection MongoDB::createCollection ( string $name [, bool $capped = FALSE [, int $size = 0 [, int $max = 0 ]]] ) public array MongoDB::createDBRef ( string $collection , mixed $a ) public array MongoDB::drop ( void ) public array MongoDB::dropCollection ( mixed $coll ) public array MongoDB::execute ( mixed $code [, array $args = array() ] ) public bool MongoDB::forceError ( void ) public MongoCollection MongoDB::__get ( string $name ) public array MongoDB::getDBRef ( array $ref ) public MongoGridFS MongoDB::getGridFS ([ string $prefix = "fs" ] ) public int MongoDB::getProfilingLevel ( void ) public bool MongoDB::getSlaveOkay ( void ) public array MongoDB::lastError ( void ) public array MongoDB::listCollections ( void ) public array MongoDB::prevError ( void ) public array MongoDB::repair ([ bool $preserve_cloned_files = FALSE [, bool $backup_original_files = FALSE ]] ) public array MongoDB::resetError ( void ) public MongoCollection MongoDB::selectCollection ( string $name ) public int MongoDB::setProfilingLevel ( int $level ) public bool MongoDB::setSlaveOkay ([ bool $ok = true ] ) public string MongoDB::__toString ( void ) } |
|||
Mongodb java 操作 | mongodb java | mongoDB JAVA操作 | |
package com.tmg.java; import java.util.regex.Pattern; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; /** * @author duanzc * @date:2010-12-23 下午05:34:24 * @version :1.0 * @类说明: */ public class MyTest { public static void main(String[] args) { try { Mongo conn = new Mongo("127.0.0.1"); // 创建数据库连接 DB local = conn.getDB("local"); // 得到数据库 boolean auth = local.authenticate("doc1", "110210121".toCharArray()); // 用户验证 if(auth){ // 如果用户名密码验证成功 System.out.println("成功......."); DBCollection col = local.getCollection("collor"); // 获得表 select(col); } } catch (Exception e) { e.printStackTrace(); } } /** 查询 */ private static void select(DBCollection col) { DBObject conditions = new BasicDBObject(); // 条件类 Integer[] num = {10,99}; // conditions.put("collor", "red"); // 等于 // conditions.put("size", new BasicDBObject("$gt", 5)); // gt(>), lt(<), gte(>=), lte(<=), in(包含), nin((不包含) // conditions.put("size", new BasicDBObject("$gt", 5).append("$lt", 15)); // >5 and <15, (可以多个条件) // conditions.put("num", new BasicDBObject("$in", num)); // 无效 // conditions.put("properties.skills", "java"); // 子查询:list // conditions.put("properties.age", 23); // 子查询:map // DBCursor ite = col.find().skip(1).limit(2); // 取范围 DBCursor ite = col.find(conditions); // 结果集(属性条件:无条件查询所有) DBCursor ite_ = col.find(new BasicDBObject // 结果集(直接条件) ("$where", "this.size==10")); //>, <, >=, <=, == while(ite.hasNext()){ System.out.println(ite.next()); } System.out.println("............"); // =========== 模糊查询:只能用正则 ============ Pattern john = Pattern.compile("ed"); // 正则 BasicDBObject query = new BasicDBObject("collor", john); DBCursor ite2 = col.find(query); while(ite2.hasNext()){ System.out.println(ite2.next()); } } } |