Skip to content

Commit f5f7ae1

Browse files
committed
Update LaravelTwStreetname.php
add different cache
1 parent cb83ce3 commit f5f7ae1

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/Seta0909/LaravelTwStreetname/LaravelTwStreetname.php

+42-12
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,42 @@ class LaravelTwStreetname
1717
private static $countrys;
1818
private static $streets;
1919
private static $zipCode;
20+
private static $cache;
2021

2122
private static function getInstance()
2223
{
2324
if (!isset(self::$instance)) {
2425
$class = __CLASS__;
2526
self::$instance = new $class();
2627
//先載載入快取
27-
28-
self::$originData = apc_fetch('LaravelTwStreetnameOrigin');
29-
self::$citys = apc_fetch('LaravelTwStreetnameCitys');
30-
self::$countrys = apc_fetch('LaravelTwStreetnameCountrys');
31-
self::$streets = apc_fetch('LaravelTwStreetnameStreets');
32-
self::$zipCode = apc_fetch('LaravelTwStreetnameZipCode');
28+
if(function_exists('opcache_compile_file'))
29+
{
30+
self::$cache = 'opcache';
31+
opcache_compile_file(dirname(__FILE__) . "/LaravelTwStreetname.php");
32+
}
33+
else if(function_exists('apc_fetch'))
34+
{
35+
self::$cache = 'apcache';
36+
self::$originData = apc_fetch('LaravelTwStreetnameOrigin');
37+
self::$citys = apc_fetch('LaravelTwStreetnameCitys');
38+
self::$countrys = apc_fetch('LaravelTwStreetnameCountrys');
39+
self::$streets = apc_fetch('LaravelTwStreetnameStreets');
40+
self::$zipCode = apc_fetch('LaravelTwStreetnameZipCode');
41+
}
42+
else
43+
{
44+
self::$cache = 'none';
45+
}
46+
3347
//載入街道Json資料
3448
if (!is_array(self::$originData)) {
3549
$streetString = file_get_contents(dirname(__FILE__) . "/address_data.json");
3650
self::$originData = json_decode($streetString, true);
37-
apc_store('LaravelTwStreetnameOrigin', self::$originData);
51+
if(self::$cache=='apcache')
52+
{
53+
apc_store('LaravelTwStreetnameOrigin', self::$originData);
54+
}
55+
3856
}
3957
//載入郵遞區號Json資料
4058
if (!is_array(self::$zipCode)) {
@@ -45,7 +63,10 @@ private static function getInstance()
4563
$temp[$val['country']] = $val['mailcode'];
4664
}
4765
self::$zipCode = $temp;
48-
apc_store('LaravelTwStreetnameZipCode', self::$zipCode);
66+
if(self::$cache=='apcache')
67+
{
68+
apc_store('LaravelTwStreetnameZipCode', self::$zipCode);
69+
}
4970
}
5071

5172
//初始化資料
@@ -56,14 +77,20 @@ private static function getInstance()
5677
self::$citys[] = $val;
5778
}
5879
}
59-
apc_store('LaravelTwStreetnameCitys', self::$citys);
80+
if(self::$cache=='apcache')
81+
{
82+
apc_store('LaravelTwStreetnameCitys', self::$citys);
83+
}
6084
}
6185
//載入鄉鎮區
6286
if (!is_array(self::$countrys)) {
6387
foreach (self::$citys as $key => $val) {
6488
self::$countrys[$val['uid']] = self::searchLink($val['uid']);
6589
}
66-
apc_store('LaravelTwStreetnameCountrys', self::$countrys);
90+
if(self::$cache=='apcache')
91+
{
92+
apc_store('LaravelTwStreetnameCountrys', self::$countrys);
93+
}
6794
}
6895
//載入街道
6996
if (!is_array(self::$streets)) {
@@ -72,7 +99,10 @@ private static function getInstance()
7299
self::$streets[$val['uid']] = self::searchLink($val['uid']);
73100
}
74101
}
75-
apc_store('LaravelTwStreetnameStreets', self::$streets);
102+
if(self::$cache=='apcache')
103+
{
104+
apc_store('LaravelTwStreetnameStreets', self::$streets);
105+
}
76106
}
77107
}
78108
}
@@ -143,4 +173,4 @@ public static function getCode($country)
143173
self::getInstance();
144174
return (isset(self::$zipCode[$country])) ? self::$zipCode[$country] : '';
145175
}
146-
}
176+
}

0 commit comments

Comments
 (0)