Skip to content

Commit 6a9a81c

Browse files
authored
Add files via upload
1 parent b821a3c commit 6a9a81c

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "webman/think-cache",
3+
"type": "library",
4+
"license": "MIT",
5+
"require": {
6+
"workerman/webman-framework": "^1.2.1",
7+
"topthink/think-cache": "^2.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Webman\\ThinkCache\\": "src"
12+
}
13+
}
14+
}

src/Install.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace Webman\ThinkCache;
3+
4+
class Install
5+
{
6+
const WEBMAN_PLUGIN = true;
7+
8+
/**
9+
* @var array
10+
*/
11+
protected static $pathRelation = [
12+
'config/thinkcache.php' => 'config/thinkcache.php'
13+
];
14+
15+
/**
16+
* Install
17+
* @return void
18+
*/
19+
public static function install()
20+
{
21+
$config_file = config_path() . '/bootstrap.php';
22+
$config = include $config_file;
23+
if(!in_array(ThinkCache::class, $config ?? [])) {
24+
$config_file_content = file_get_contents($config_file);
25+
$config_file_content = preg_replace('/\];/', " Webman\ThinkCache\ThinkCache::class,\n];", $config_file_content);
26+
file_put_contents($config_file, $config_file_content);
27+
}
28+
/*$thinkcache_file = config_path() . '/thinkcache.php';
29+
if (!is_file($thinkcache_file)) {
30+
copy(__DIR__ . '/config/thinkcache.php', $thinkcache_file);
31+
}*/
32+
static::installByRelation();
33+
}
34+
35+
/**
36+
* Uninstall
37+
* @return void
38+
*/
39+
public static function uninstall()
40+
{
41+
$config_file = config_path() . '/bootstrap.php';
42+
$config = include $config_file;
43+
if(in_array(ThinkCache::class, $config ?? [])) {
44+
$config_file = config_path() . '/bootstrap.php';
45+
$config_file_content = file_get_contents($config_file);
46+
$config_file_content = preg_replace('/ {0,4}Webman\\\\ThinkCache\\\\ThinkCache::class,?\r?\n?/', '', $config_file_content);
47+
file_put_contents($config_file, $config_file_content);
48+
}
49+
self::uninstallByRelation();
50+
}
51+
52+
/**
53+
* installByRelation
54+
* @return void
55+
*/
56+
public static function installByRelation()
57+
{
58+
foreach (static::$pathRelation as $source => $dest) {
59+
if ($pos = strrpos($dest, '/')) {
60+
$parent_dir = base_path().'/'.substr($dest, 0, $pos);
61+
if (!is_dir($parent_dir)) {
62+
mkdir($parent_dir, 0777, true);
63+
}
64+
}
65+
//symlink(__DIR__ . "/$source", base_path()."/$dest");
66+
copy_dir(__DIR__ . "/$source", base_path()."/$dest");
67+
echo "Create $dest
68+
";
69+
}
70+
}
71+
72+
/**
73+
* uninstallByRelation
74+
* @return void
75+
*/
76+
public static function uninstallByRelation()
77+
{
78+
foreach (static::$pathRelation as $source => $dest) {
79+
$path = base_path()."/$dest";
80+
if (!is_dir($path) && !is_file($path)) {
81+
continue;
82+
}
83+
echo "Remove $dest
84+
";
85+
if (is_file($path) || is_link($path)) {
86+
unlink($path);
87+
continue;
88+
}
89+
remove_dir($path);
90+
}
91+
}
92+
93+
}

src/ThinkCache.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Webman\ThinkCache;
4+
5+
use Webman\Bootstrap;
6+
use Workerman\Timer;
7+
use think\facade\Cache;
8+
9+
class ThinkCache implements Bootstrap
10+
{
11+
public static function start($worker)
12+
{
13+
$config = config('thinkcache');
14+
if (!$config) {
15+
return;
16+
}
17+
Cache::config($config);
18+
if ($worker && $config['default'] === 'redis') {
19+
Timer::add(55, function () {
20+
Cache::get('ping');
21+
});
22+
}
23+
}
24+
}

src/config/thinkcache.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
return [
3+
'default' => 'file',
4+
'stores' => [
5+
'file' => [
6+
'type' => 'File',
7+
// 缓存保存目录
8+
'path' => runtime_path() . '/cache/',
9+
// 缓存前缀
10+
'prefix' => '',
11+
// 缓存有效期 0表示永久缓存
12+
'expire' => 0,
13+
],
14+
'redis' => [
15+
'type' => 'redis',
16+
'host' => '127.0.0.1',
17+
'port' => 6379,
18+
'prefix' => '',
19+
'expire' => 0,
20+
],
21+
],
22+
];

0 commit comments

Comments
 (0)