DEV Community

vic
vic

Posted on • Edited on

A minimalist high-performance php framework ,can respond to requests within 1ms

One - A minimalist high-performance php framework that supports the [swoole | php-fpm] environment

  • Fast - can respond to requests within 1ms even under php-fpm
  • Simple - let you focus on what you do with one instead of how to use one
  • Flexible - each component is loosely coupled, can be flexibly matched and used, and the method of use is consistent
    • Native sql can be used with model relation with, relation can cross database types
    • session can be used under http, websocket or even tcp, udp and cli
    • ...
  • High efficiency - operational performance, development efficiency, and easy maintenance.
  • Lightweight - no other dependencies, the total code amount of all components from routing and orm does not exceed 500k. If there is no complicated calling relationship in secondary development, you can quickly grasp the design principle

hello world

install

composer create-project lizhichao/one-app app
cd app
php App/swoole.php 

# stop : `php App/swoole.php -o stop`  
# reload : `php App/swoole.php -o reload`  
# Daemon start : `php App/swoole.php -o start`  

curl http://127.0.0.1:8081/
Enter fullscreen mode Exit fullscreen mode

performance

reference:

Main components

  • Router
    • Support greedy matching and priority
    • Support ws/tcp/http……any protocol
    • Good performance, adding tens of thousands of routes will not reduce the parsing performance
    • Routing grouping, middleware...all there should be
  • ORM
    • Support database: mysql, clickHouse,
    • Relation processing: one-to-one, one-to-many, many-to-one, polymorphism... There are various relations, which can be related across database types
    • Cache: Automatically refresh data, support configuration of various cache granularities
    • Event: All operations can be captured, including you use native SQL to operate the database
    • Database connection: synchronous, asynchronous, blocking, disconnection and reconnection are all supported
    • sql template: automatically generate template id, you can understand what types of sql the project has, and the proportion of the number of calls, and provide data support for later data optimization.
    • Statement reuse: Provide SQL execution performance
  • rpc
    • Can automatically generate remote method mapping, support ide prompt
    • Direct call mapping method == call remote method, support chain call
    • Support rpc middleware, authentication, encryption and decryption, caching...
  • Log
    • Complete information: record the complete file name + line number to quickly locate the code location
    • requestId: You can easily view the entire request log information and service relationship

Router


Router::get('/', \App\Controllers\IndexController::class . '@index');

// router with params
Router::get('/user/{id}', \App\Controllers\IndexController::class . '@user');

// router with group
Router::group(['namespace'=>'App\\Test\\WebSocket'],function (){
    // websocket router
    Router::set('ws','/a','TestController@abc'); 
    Router::set('ws','/b','TestController@bbb'); 
});

// Middleware
Router::group([
    'middle' => [
        \App\Test\MixPro\TestMiddle::class . '@checkSession'
    ]
], function () {
    Router::get('/mix/ws', HttpController::class . '@ws');
    Router::get('/user/{id}', \App\Controllers\IndexController::class . '@user');
    Router::post('/mix/http/send', HttpController::class . '@httpSend');
});

Enter fullscreen mode Exit fullscreen mode

orm

Define the model

namespace App\Model;

use One\Database\Mysql\Model;

// There is no need to specify the primary key in the model, the framework will cache the database structure
// Automatically match the primary key, automatically filter the fields in the non-table structure
class User extends Model
{
    // Define the table name corresponding to the model
    CONST TABLE = 'users';

    // define relationship
    public function articles()
    {
        return $this->hasMany('id',Article::class,'user_id');
    }

    // define event
    // Whether to enable automatic caching
    // ……
}
Enter fullscreen mode Exit fullscreen mode

Use model

  • The database connection is a single column under fpm,
  • All database operations are automatically switched to connection pool in swoole mode
// Query a record
$user = User::find(1);

// Related query
$user_list = User::whereIn('id',[1,2,3])->with('articles')->findAll()->toArray();

// update
$r = $user->update(['name' => 'aaa']);
// or
$r = user::where('id',1)->update(['name' => 'aaa']);
// $r To influence the number of records

Enter fullscreen mode Exit fullscreen mode

Cache

// Set cache without expiration time
Cache::set('ccc',1);

// Set the cache to expire in 1 minute
Cache::set('ccc',1,60);


Cache::get('ccc');

// or cache ccc expires 10s under tag1
Cache::get('ccc',function (){
    return 'info';
},10,['tag1']);

// Refresh all caches under tag1
Cache::flush('tag1');

Enter fullscreen mode Exit fullscreen mode

Code warehouse

https://github.com/lizhichao/one

Top comments (0)