生活随笔
收集整理的這篇文章主要介紹了
PHP框架 Phalcon 1.0.0 beta发布,实测性能强劲
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們宣布今天(時差問題,應該是昨天了)發布Phalcon 1.0.0 beta版本,發布此版本,主要是從社區得到測試反饋加以改進。此版本引入了一些比較重要的特性:
多級緩存:
這個新功能是緩存組件的一部分,允許開發人員來實現一個多級緩存。這個新功能非常有用,因為你可以保存相同的數據在多個后端緩存組件中,并可配置不同的生命周期,讀取緩存從最快的適配器到最慢的一個,直到數據都已經過期:
| 02 | $ultraFastFrontend = new Phalcon\Cache\Frontend\Data(array( |
| 06 | $fastFrontend = new Phalcon\Cache\Frontend\Data(array( |
| 07 | ????"lifetime" => 86400 |
| 10 | $slowFrontend = new Phalcon\Cache\Frontend\Data(array( |
| 11 | ????"lifetime" => 604800 |
| 14 | //Backends are registered from the fastest to the slower |
| 15 | $cache = new \Phalcon\Cache\Multiple(array( |
| 16 | ????new Phalcon\Cache\Backend\Apc($ultraFastFrontend, array( |
| 17 | ????"prefix" => 'cache', |
| 19 | ??new Phalcon\Cache\Backend\Memcache($fastFrontend, array( |
| 20 | ????"prefix" => 'cache', |
| 21 | ????"host" => "localhost", |
| 24 | ??new Phalcon\Cache\Backend\File($slowFrontend, array( |
| 25 | ????"prefix" => 'cache', |
| 26 | ????"cacheDir" => "../app/cache/" |
| 30 | //Save, saves in every backend |
| 31 | $cache->save('my-key', $data); |
Volt模板引擎改進
此版本引入的一些Volt改進特性:
| 02 | {{ total > 0 ? total|format('%0.2f') : '0.0' }} |
| 05 | {% for robot in robots %} |
| 08 | ????There are no robots |
| 13 | {% for robot in robots %} |
| 14 | ????{% if loop.first %} |
| 17 | ????????????????<th>Position</th> |
| 18 | ????????????????<th>Id</th> |
| 19 | ????????????????<th>Name</th> |
| 25 | ????????<th>{{ loop.index }}</th> |
| 26 | ????????<th>{{ robot.id }}</th> |
| 27 | ????????<th>{{ robot.name }}</th> |
| 35 | {# Space control delimiters #} |
| 37 | ????{%- for robot in robots -%} |
| 38 | ????<li>? {{- robot.name -}}</li> |
垂直/水平分片的改進
現在,你可以定義不同的數據庫連接,使之一個只用于讀操作,另一個只用于寫操作。對于RDBMS中使用主從方式的應用場景非常有用:
| 1 | class Robots extends Phalcon\Mvc\Model |
| 3 | ????public function initialize() |
| 5 | ????????$this->setReadConnectionService('dbSlave'); |
| 6 | ????????$this->setWriteConnectionService('dbMaster'); |
在大中型項目中,在數據庫設計的時候,考慮到數據庫最大承受數據量,通常會把數據庫或者數據表水平切分,以降低單個庫,單個表的壓力。
因此,水平切片意味著讀取數據將根據條件進行數據查詢:
| 01 | class Robots extends Phalcon\Mvc\Model |
| 03 | ????public function selectReadConnection($intermediate, $bindParams, $bindTypes) |
| 05 | ????????//Check if there is a 'where' clause in the select |
| 06 | ????????if (isset($intermediate['where'])) { |
| 08 | ????????????$conditions = $intermediate['where']; |
| 10 | ????????????//Choose the possible shard according to the conditions |
| 11 | ????????????if ($conditions['left']['name'] == 'id') { |
| 12 | ????????????????$id = $conditions['right']['value']; |
| 13 | ????????????????if ($id > 0 && $id < 10000) { |
| 14 | ????????????????????return $this->getDI()->get('dbShard1'); |
| 16 | ????????????????if ($id > 10000) { |
| 17 | ????????????????????return $this->getDI()->get('dbShard2'); |
| 22 | ????????//Use a default shard |
| 23 | ????????return $this->getDI()->get('dbShard0'); |
記錄快照
有了這項新功能,指定的Models可以設定為查詢時保持記錄的快照。你可以使用此功能來實現審計或只是為了知道哪些字段被更改過:
| 1 | class Robots extends Phalcon\Mvc\Model |
| 3 | ????public function initalize() |
| 5 | ????????$this->keepSnapshots(true); |
你可以通過以下方式檢測哪些字段被更改過:
| 2 | $robot->name = 'Other name'; |
| 3 | var_dump($robot->getChangedFields()); // ['name'] |
| 4 | var_dump($robot->hasChanged('name')); // true |
| 5 | var_dump($robot->hasChanged('type')); // false |
動態更新
此功能允許ORM在創建SQL UPDATE語句時,只改變有改變的字段,而不是整個表的所有字段。在某些情況下,這可以提高性能,減少應用程序與數據庫服務器之間的傳輸數據量:
| 1 | class Robots extends Phalcon\Mvc\Model |
| 3 | ????public function initalize() |
| 5 | ????????$this->useDynamicUpdate(true); |
驗證
Phalcon\Validation 是基于ORM,ODM驗證系統實現的一個獨立的驗證組件,該組件可以在model及collection之外實現驗證規則:
| 01 | $validation = new Phalcon\Validation(); |
| 04 | ????->add('name', new PresenceOf(array( |
| 05 | ????????'message' => 'The name is required' |
| 07 | ????->add('name', new StringLength(array( |
| 09 | ????????'minimumMessage' => 'The name is too short' |
| 11 | ????->add('email', new PresenceOf(array( |
| 12 | ????????'message' => 'The email is required' |
| 14 | ????->add('email', new Email(array( |
| 15 | ????????'message' => 'The email is not valid' |
| 17 | ????->add('login', new PresenceOf(array( |
| 18 | ????????'message' => 'The login is required' |
| 21 | $messages = $validation->validate($_POST); |
| 22 | if (count($messages)) { |
| 23 | ????foreach ($messages as $message) { |
版本 1.0.0還包括其他一些小改動,bug修復及穩定性方面的改進。你可以在此看到完整的更新日志
?
幫助測試
可以從1.0.0分支中安裝此版本:
| 1 | git clone http://github.com/phalcon/cphalcon |
Windows用戶可以直接從下載頁面下載DLL文件。
我們歡迎您提供寶貴的意見與建議,可以通過 Phosphorum, Stack Overflow or Google Group 進行交流。如果你發現了任何BUG,請在Github上創建issue.
中文文檔: http://phalcon.5iunix.net
總結
以上是生活随笔為你收集整理的PHP框架 Phalcon 1.0.0 beta发布,实测性能强劲的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。