forked from Edgio/ectoken
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_memcache.php
More file actions
61 lines (51 loc) · 2.13 KB
/
example_memcache.php
File metadata and controls
61 lines (51 loc) · 2.13 KB
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
<?php
/**
* Copyright (C) 2016 Verizon. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This example shows how to cache the output of ectoke_generate
* for expiring tokens, using memcached.
* This can be used to mitigate the impact of token generation, as a given token
* should only be generated once. Memcache handles the expiration of tokens in cache.
*
*/
$key = "12345678";
$expires = "1185943200";
$params = "ec_secure=1&ec_expire=" . $expires . "&ec_clientip=111.11.111.11&ec_country_allow=US&ec_ref_allow=ec1.com";
// Concatenate the key and plaintext to use as a key for memcache
$mc_key = $key . ':' . $params;
if (extension_loaded('memcache')){
// Connect to our local memcached
$memcache = memcache_connect('localhost', 11211);
if ($token = $memcache->get($mc_key) != false){
// It exists in memcache, so we can use it.
break;
} else {
// Check to see if the extension properly loaded before using it.
if (extension_loaded('ectoken')){
// Generate the token
$token = ectoken_encrypt_token($key, $params);
// Insert the token into the cache, with the correct expiration
// $expires is a date, as seconds since unix epoch, in the GMT timezone
// memcached needs that converted to the local time zone.
$memcache->set($mc_key, $token, date(intval($expires), 'U') );
} else {
trigger_error('The EdgeCast Token module could not be loaded.', E_USER_ERROR);
}
}
} else {
trigger_error('The memcache module could not be loaded.', E_USER_ERROR);
}
echo $token;
?>