抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

前言

接口一直让我很晕,下面这篇文章有了很好的说明.
现代 PHP 新特性系列(二) —— 善用接口
这里我就再简单化下,以便我能快速记忆.


简要说明

接口说直观点就是模具,或者说API

  • API说
    说他像API,是因为他提出来所有功能.你知道接口有什么,你就知道你能做什么了

  • 模具说
    说他像模具,是因为接口的功能是统一的,一致的.但是实现的方法可以不同.就像你做一个花瓶的模具,但是根据填充的材料不同,可以做出不同的花瓶.什么玻璃的,陶瓷的,珐琅的.虽然他们长的都一个样.

接口的作用

所以接口的作用,就是解耦.

比如一个DB类流程:

  • DB操作类,根据DB接口提供的功能,进行具体功能开发.
  • DB实例化的时候,必须传入符合DB接口的DB驱动类.(符合接口保证功能可用,而且不符合接口也会报错)
  • DB操作类,根据传入不同DB驱动实例,达到解耦的作用.(因为功能名称都是统一的)

大致如下图:
接口流程图

代码参考 用的不是DB的例子
Store类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Illuminate\Contracts\Cache;

interface Store
{
/**
* Retrieve an item from the cache by key.
*
* @param string|array $key
* @return mixed
*/
public function get($key);

/**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function many(array $keys);

/**
* Store an item in the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes);

/**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMany(array $values, $minutes);

/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function increment($key, $value = 1);

/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function decrement($key, $value = 1);

/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function forever($key, $value);

/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public function forget($key);

/**
* Remove all items from the cache.
*
* @return void
*/
public function flush();

/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix();
}

MemcachedStore类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace Illuminate\Cache;

use Memcached;
use Illuminate\Contracts\Cache\Store;

class MemcachedStore extends TaggableStore implements Store
{
/**
* The Memcached instance.
*
* @var \Memcached
*/
protected $memcached;

/**
* A string that should be prepended to keys.
*
* @var string
*/
protected $prefix;

/**
* Create a new Memcached store.
*
* @param \Memcached $memcached
* @param string $prefix
* @return void
*/
public function __construct($memcached, $prefix = '')
{
$this->setPrefix($prefix);
$this->memcached = $memcached;
}

/**
* Retrieve an item from the cache by key.
*
* @param string|array $key
* @return mixed
*/
public function get($key)
{
$value = $this->memcached->get($this->prefix.$key);

if ($this->memcached->getResultCode() == 0) {
return $value;
}
}

/**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function many(array $keys)
{
$prefixedKeys = array_map(function ($key) {
return $this->prefix.$key;
}, $keys);

$values = $this->memcached->getMulti($prefixedKeys, null, Memcached::GET_PRESERVE_ORDER);

if ($this->memcached->getResultCode() != 0) {
return array_fill_keys($keys, null);
}

return array_combine($keys, $values);
}

/**
* Store an item in the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
$this->memcached->set($this->prefix.$key, $value, $minutes * 60);
}

/**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMany(array $values, $minutes)
{
$prefixedValues = [];

foreach ($values as $key => $value) {
$prefixedValues[$this->prefix.$key] = $value;
}

$this->memcached->setMulti($prefixedValues, $minutes * 60);
}

/**
* Store an item in the cache if the key doesn't exist.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
return $this->memcached->add($this->prefix.$key, $value, $minutes * 60);
}

/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function increment($key, $value = 1)
{
return $this->memcached->increment($this->prefix.$key, $value);
}

/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function decrement($key, $value = 1)
{
return $this->memcached->decrement($this->prefix.$key, $value);
}

/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
}

/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public function forget($key)
{
return $this->memcached->delete($this->prefix.$key);
}

/**
* Remove all items from the cache.
*
* @return void
*/
public function flush()
{
$this->memcached->flush();
}

/**
* Get the underlying Memcached connection.
*
* @return \Memcached
*/
public function getMemcached()
{
return $this->memcached;
}

/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}

/**
* Set the cache key prefix.
*
* @param string $prefix
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
}
}

CacheStore类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace App\Tests;

use Illuminate\Contracts\Cache\Store;

class CacheStore
{
protected $store;

public function __construct(Store $store)
{
$this->store = $store;
}

public function get($key)
{
return $this->store->get($key);
}

public function put($key, $value, $minutes=1)
{
$this->store->put($key, $value, $minutes);
}

public function forget($key)
{
$this->store->forever($key);
}

public function flush()
{
$this->store->flush();
}
}

使用CacheStore类

1
2
3
4
5
6
$memcached = new \Memcached();
$memcached->addServer('localhost',11211);
$memcachedCache = new MemcachedStore($memcached);
$cacheStore = new CacheStore($memcachedCache);
$cacheStore->put('site','http://LaravelAcademy.org');
dd($cacheStore->get('site'));

评论