The introduction swoft
1、mysql、
2 Redis
A, mysql use:
1, configuration, app\bean.php
file
'db' => [ 'class' => Database::class, 'dsn' => 'mysql:dbname=webdemo;host=localhost', 'username' => 'root', 'password' => 'foto_cj1', ],
//链接池配置 'db2.pool' => [ 'class' => Pool::class, 'database' => bean('db'), 'minActive' => 10, 'maxActive' => 20, 'maxWait' => 0, 'maxWaitTime' => 0, 'maxIdleTime' => 60, ],
2, generates Model, a Model corresponds to a table. In / App / Model / Entity / folder new file
<PHP DECLARE (strict_types = 1? ); namespace App \ Model \ the Entity; use Swoft \ Db \ Annotation \ Mapping \ Column; use Swoft \ Db \ Annotation \ Mapping \ the Entity; use Swoft \ Db \ Annotation \ Mapping \ Id; Swoft use \ Db \ Eloquent \ the Model; / ** * * Class Demo * * @Since 2.0 * * @Entity (table = " Demo " , the pool = " db2.pool " ) // define Model, the parameters corresponding table and connection pool (optional) * / class Demo the extends the Model { / ** * default and added automatically created_at updated_at, when not required to set to false * @var BOOL */ public $modelTimestamps = false; /** * * @Id(incrementing=false) * @Column(name="id") //定义列 * * @var int */ private $id; /** * @Column(name="name") * * @var string|null */ private $name; /** * @param int $id * * @return void */ public function setId(int $id): void { $this->id = $id; } /** * @param string|null $name * * @return void */ public function setName(?string $name): void { $this->name = $name; } /** * @return int */ public function getId(): ?int { return $this->id; } /** * @return string|null */ public function getName(): ?string { return $this->name; } }
3, using (Model)
//查询 $user = Demo::find($)->toArray(); //修改 $sdbuser = Demo::find($id); $sdbuser->setName("cjcjcjccj"); //或者 $sdbuser->update(['name' => "dddddd"]); //删除 $result = Demo::where('id', 1)->delete(); $user = Demo::find($data["uid"]); $result = $user->delete(); //插入 $count = new Demo(); $count->setId(mt_rand(1, 100)); $count->setName('attr'); $result = $count->save(); $nId = $count->getId(); //批量插入 $insArr = [ [ 'id' => random_int(1, 100), 'name' => md5(uniqid()) ], [ 'id' => random_int(1, 100), 'name' => md5(uniqid()) ] ]; $result = Demo::insert($insArr);
4, DB native use: https://www.swoft.org/docs/2.x/zh-CN/db/builder.html
Two: Redis is simple to use:
1, configuration: app\bean.php
file
'redis' => [ 'class' => RedisDb::class, 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, 'option' => [ 'prefix' => 'swoft:' ] ], 'redis.pool' => [ 'class' => \Swoft\Redis\Pool::class, 'redisDb' => \bean('redis'), 'minActive' => 10, 'maxActive' => 20, 'maxWait' => 0, 'maxWaitTime' => 0, 'maxIdleTime' => 40, ]
2, using
(1), directly
Redis::set($key, $setData,$time);
Redis::get($key);
(2), by way of the connection pool to use injection @Inject
In / App / Http / Controller / in New File
/** * Class RedisController * * @since 2.0 * @Controller("redis") */ class RedisController { /** * @Inject("redis.pool") * * @var Pool */ private $redis; /** * @return array * @RequestMapping("find") //访问路由: /redis/find * @throws \Throwable */ public function find() { $us = $this->redis->get('user'); if($us) $this->redis->set('user', ["name" => "gimi", "age" => "18"],120); return $us; } }
Three: Task task:
1, arranged, adding app / bean.php file
'httpServer' => [ // ... 'on' => [ SwooleEvent::TASK => \bean(TaskListener::class), // Enable task must task and finish event SwooleEvent::FINISH => \bean(FinishListener::class) ], /* @see HttpServer::$setting */ 'setting' => [ 'task_worker_num' => 12, 'task_enable_coroutine' => true, 'worker_num' => 2 ] ],
2, regular tasks
1, the require installation swoft Composer / the crontab 2 , disposed ' HTTPServer ' => [ // ... ' Process ' => [ ' the crontab ' => the bean (Swoft \ the Crontab \ Process \ CrontabProcess :: class ) ], // ... ], 3, defined in / app / Task / Crontab / file New folder < PHP? namespace App \ Task \ Crontab; use Swoft \ Crontab \ annotaion \ Mapping \ Cron; use Swoft \ Crontab \ annotaion \ Mapping \ Scheduled; / ** *DemoCronTask Class * @package App \ Task \ Crontab * * @Scheduled (name = " demoCronTask " ) // declare a scheduled task * / class DemoCronTask { / ** * @Cron ( " * " ) // execute per second * / public SecondTask function () { var_dump ( " --111 ---- " , DATE ( ' Ymd H: I: S ' , Time ())); } / ** * @Cron ( " 0 * * * * * " ) // every minute * / public function miunteTask(){ var_dump("222------",date('Y-m-d H:i:s', time())); } }
3, coroutine, asynchronous tasks
(1), a mission statement, in / app / Task / Task / Folder New
<?php declare(strict_types=1); namespace App\Task\Task; use Swoft\Task\Annotation\Mapping\Task; use Swoft\Task\Annotation\Mapping\TaskMapping; /** * Class DemoTask * * @since 2.0 * @Task(name="demoV2Task") //标记类是一个任务 */ class DemoTask { /** * @TaskMapping(name="list") //映射名称 * * @param int $id * @param string $default * * @return array */ public function getList(int $id): array { var_dump("------------"); sleep(5); return [ 'list' => [1, 3, 3], 'id' => $id ]; } /** * @param int $id * @return bool * * @TaskMapping(name="putLists") */ public function putList(int $id) : bool { if($id > 5) return true; return false; } }
(2), task delivery
// coroutine delivery $ Data = the Task :: CO.'S ( ' demoV2Task ' , ' List ' , [12 is ]); // asynchronous delivery $ Data = the Task :: the async ( ' demoV2Task ' , ' List ' , [12 is]) ;
(3), asynchronous delivery if the need to focus on asynchronous task processing result, listeners can be added in the folder / app / Task / Listener / under New File
<?php namespace App\Task\Listener; use Swoft\Log\Helper\CLog; use function context; use Swoft\Event\Annotation\Mapping\Listener; use Swoft\Event\EventHandlerInterface; use Swoft\Event\EventInterface; use Swoft\Task\TaskEvent; /** * Class DemoListener * @package App\Task\Listener * @Listener(event=TaskEvent::FINISH) //参数必须带Finsh */ class DemoListener implements EventHandlerInterface{ /** * @param EventInterface $event * * @throws \Swoft\Exception\SwoftException */ public function handle(EventInterface $event): void { // TODO: Implement handle() method. $fId = context()->getTaskUniqid(); // var_dump($fId); CLog::info(\context()->getTaskUniqid()); $taskData = context()->getTaskData(); // var_dump($taskData); CLog::info(\context()->getTaskData()); } }
Note: Task is not yet clear there are two places
1, when the delivery is blocked coroutine.
2, asynchronous monitor where all listeners only with the parameters @Listener (event = TaskEvent :: FINISH) will be executed again.
View the document:
https://www.swoft.org/docs/2.x/zh-CN/db/index.html
https://www.swoft.org/docs/2.x/zh-CN/redis/index.html
https://www.swoft.org/docs/2.x/zh-CN/task/index.html