PHP + Redis is the most common one
1. Install Redis, and install the redis extension according to your PHP version
First look at phpInfo (); PHP environment Information
Download Redis
https://windows.php.net/downloads/pecl/snaps/redis/
https://windows.php.net/downloads/pecl/releases/igbinary/
Copy the code
Be sure to make sure that the downloaded version corresponds to PHP
3. Copy php_redis. DLL and php_redis. PDB to the ext directory in PHP
PHP. Ini file (PS: PHP. Ini file is in Apache directory)
; php_redis
extension=php_igbinary.dll
extension=php_redis.dll
Copy the code
Note: Extension =php_igbinary. DLL must be placed before extension=php_redis
5. Restart Apache and run phpInfo to check whether the extension is successfully installed
I used ThinkPHP5.0 to configure Redis in config
Then create a new module folder under extend to create the redis.php file
<?php
/**
* Created by PhpStorm.
* User: lhl
* Date: 2018/8/20
* Time: 下午1:52
*/
namespace module;
class Redis extends \Redis
{
public static function redis() {
$con = new \Redis();
$con->connect(config('redis.host'), config('redis.port'), 5);
return $con;
}
}
在thinkphp文件下的helper.php加入
if (!function_exists('redis')) {
/**
* 获取容器对象实例
* @return Container
*/
function redis()
{
return \module\Redis::redis();
}
}
然后就可以在控制器写redis缓存方法了
//创建redis缓存
\Cache::store('redis')->set(key, value);
//读取缓存
\Cache::store('redis')->get(key);
Copy the code
The core problem of seckilling is that it will not exceed the purchase of inventory under the condition of large concurrency, which is the key to deal with. So the idea is to do some basic data generation in the first step of seckilling: three tables to test, namely: commodity table, log table, order table,
/// seckill entry
public function insva(){ $id = input('id'); // Get the commodity id if(! $id){ return $this->insertlog(0); $redis = $this->redis(); $count = $redis->reduceStock('goods_stock'); If ($count ==0){$this->insertlog(0); Return false; }else{ $order = $this->build_order_no(); $status = 1; $data = db('goods')->where('id',$id)->find(); if (! $data){ return $this->insertlog(0); / / goods does not exist} $res = db (" order ") - > insert ([' order_sn '= > $order,' the uid = > $this - > user_id, 'goods_id' = > $id]); / / insert order $stock = db (" goods ") - > where (' id ', $id) - > setDec (" count "); If ($stock){$this->insertlog(); }else{$this->insertlog(0); Public function redisinit(){$store=50; $this->redis(); $this->redis(); $redis->del('goods_store'); $res=$redis->llen('goods_store'); $count=$store-$res; for($i=0; $i<$count; $i++){ $redis->lpush('goods_store',1); // List advances 50, Function build_order_no(){return date('ymd'). Substr (implode(NULL, array_map('ord',)) str_split(substr(uniqid(), 7, 13), 1))), 0, 8); Function insertlog($status=1){return Db::name("ab_log")->insertGetId(["count"=>1,"status"=>$status,"addtime"=>date('Y-m-d H:i:s')]); }Copy the code
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you