accelerator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -10,21 +10,23 @@ Drop-in page caching using nginx, lua, and memcached.
|
|
10
10
|
|
11
11
|
##Requirements
|
12
12
|
|
13
|
-
|
13
|
+
You'll need an nginx build with the following modules:
|
14
14
|
|
15
15
|
* [LuaJIT](http://wiki.nginx.org/HttpLuaModule)
|
16
16
|
* [MemcNginxModule](http://wiki.nginx.org/HttpMemcModule)
|
17
17
|
* [LuaRestyMemcachedLibrary](https://github.com/agentzh/lua-resty-memcached)
|
18
18
|
|
19
|
-
|
19
|
+
##Configure nginx
|
20
20
|
|
21
|
-
|
21
|
+
###Install luarock
|
22
22
|
|
23
|
-
|
23
|
+
git clone git://github.com/winton/nginx-accelerator.git
|
24
|
+
cd nginx-accelerator
|
25
|
+
luarocks make
|
24
26
|
|
25
|
-
|
27
|
+
###nginx.conf
|
26
28
|
|
27
|
-
Drop the following line in any `location` directive
|
29
|
+
Drop the following line in any `location` directive:
|
28
30
|
|
29
31
|
access_by_lua "require('accelerator').access()";
|
30
32
|
|
@@ -57,7 +59,7 @@ To configure your memcached connection information:
|
|
57
59
|
cache = Accelerator.new("localhost:11211")
|
58
60
|
cache.get("/test")
|
59
61
|
cache.set("/test", "body")
|
60
|
-
cache.delete("/test")
|
62
|
+
cache.delete("/test")
|
61
63
|
cache.expire("/test", 10)
|
62
64
|
|
63
65
|
## Running specs
|
data/lib/accelerator.lua
CHANGED
@@ -38,7 +38,7 @@ access = function(opts)
|
|
38
38
|
local fn
|
39
39
|
fn = function()
|
40
40
|
local memc = memclient(opts)
|
41
|
-
local cache, flags, err = memc:get(ngx.var.
|
41
|
+
local cache, flags, err = memc:get(ngx.var.uri)
|
42
42
|
if err then
|
43
43
|
error(err)
|
44
44
|
end
|
@@ -56,8 +56,8 @@ access = function(opts)
|
|
56
56
|
local co = coroutine.create(function()
|
57
57
|
cache = cache or { }
|
58
58
|
cache.time = os.time()
|
59
|
-
memc:set(ngx.var.
|
60
|
-
local res = ngx.location.capture(ngx.var.
|
59
|
+
memc:set(ngx.var.uri, json.encode(cache))
|
60
|
+
local res = ngx.location.capture(ngx.var.uri)
|
61
61
|
if not res then
|
62
62
|
return
|
63
63
|
end
|
@@ -76,7 +76,7 @@ access = function(opts)
|
|
76
76
|
end
|
77
77
|
res.time = os.time()
|
78
78
|
res.ttl = ttl or opts.ttl or 10
|
79
|
-
memc:set(ngx.var.
|
79
|
+
memc:set(ngx.var.uri, json.encode(res))
|
80
80
|
return debug("write cache")
|
81
81
|
end)
|
82
82
|
coroutine.resume(co)
|
data/lib/accelerator.rb
CHANGED
@@ -15,8 +15,8 @@ class Accelerator
|
|
15
15
|
|
16
16
|
def expire(uri, ttl=nil)
|
17
17
|
if data = get_and_set_time(uri)
|
18
|
-
data[:ttl]
|
19
|
-
data[:time] -= data[:ttl]
|
18
|
+
data[:ttl] = ttl if ttl
|
19
|
+
data[:time] -= data[:ttl]
|
20
20
|
@memc.set(key(uri), data.to_json, 604800, false)
|
21
21
|
end
|
22
22
|
end
|
@@ -27,8 +27,9 @@ class Accelerator
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
def set(uri, body)
|
31
|
-
data
|
30
|
+
def set(uri, body, ttl=nil)
|
31
|
+
data = get_and_set_time(uri)
|
32
|
+
data ||= { :time => Time.now.to_i, :ttl => ttl || 10 }
|
32
33
|
data[:body] = body
|
33
34
|
@memc.set(key(uri), data.to_json, 604800, false)
|
34
35
|
end
|
data/spec/spec_helper.rb
CHANGED
data/src/accelerator.moon
CHANGED
@@ -40,7 +40,7 @@ access = (opts) ->
|
|
40
40
|
fn = ->
|
41
41
|
memc = memclient(opts)
|
42
42
|
|
43
|
-
cache, flags, err = memc\get(ngx.var.
|
43
|
+
cache, flags, err = memc\get(ngx.var.uri)
|
44
44
|
error(err) if err
|
45
45
|
|
46
46
|
if cache
|
@@ -60,10 +60,10 @@ access = (opts) ->
|
|
60
60
|
-- Immediately update the time to prevent multiple writes
|
61
61
|
cache = cache or {}
|
62
62
|
cache.time = os.time()
|
63
|
-
memc\set(ngx.var.
|
63
|
+
memc\set(ngx.var.uri, json.encode(cache))
|
64
64
|
|
65
65
|
-- Make subrequest
|
66
|
-
res = ngx.location.capture(ngx.var.
|
66
|
+
res = ngx.location.capture(ngx.var.uri)
|
67
67
|
return if not res
|
68
68
|
|
69
69
|
-- Parse TTL
|
@@ -81,7 +81,7 @@ access = (opts) ->
|
|
81
81
|
res.ttl = ttl or opts.ttl or 10
|
82
82
|
|
83
83
|
-- Write cache
|
84
|
-
memc\set(ngx.var.
|
84
|
+
memc\set(ngx.var.uri, json.encode(res))
|
85
85
|
debug("write cache")
|
86
86
|
|
87
87
|
coroutine.resume(co)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: accelerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -56,12 +56,12 @@ files:
|
|
56
56
|
- LICENSE
|
57
57
|
- README.md
|
58
58
|
- Rakefile
|
59
|
+
- accelerator-1.0-1.rockspec
|
60
|
+
- accelerator.gemspec
|
59
61
|
- bin/accelerator
|
60
62
|
- build
|
61
63
|
- lib/accelerator.lua
|
62
64
|
- lib/accelerator.rb
|
63
|
-
- nginx-accelerator-1.0-1.rockspec
|
64
|
-
- nginx-accelerator.gemspec
|
65
65
|
- nginx/nginx.conf
|
66
66
|
- nginx/start
|
67
67
|
- spec/accelerator_spec.rb
|