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
- Nginx build with the following modules:
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
- See the [Building OpenResty](#building-openresty) section below for instructions.
19
+ ##Configure nginx
20
20
 
21
- ##Install
21
+ ###Install luarock
22
22
 
23
- luarocks install nginx-accelerator
23
+ git clone git://github.com/winton/nginx-accelerator.git
24
+ cd nginx-accelerator
25
+ luarocks make
24
26
 
25
- ##Nginx config
27
+ ###nginx.conf
26
28
 
27
- Drop the following line in any `location` directive within `nginx.conf`:
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
@@ -1,4 +1,4 @@
1
- package = "nginx-accelerator"
1
+ package = "accelerator"
2
2
  version = "1.0-1"
3
3
  source = {
4
4
  url = "http://github.com/winton/nginx-accelerator"
@@ -6,7 +6,7 @@ $:.unshift lib unless $:.include?(lib)
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "accelerator"
9
- s.version = '0.1.0'
9
+ s.version = '0.1.1'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = [ "Winton Welsh" ]
12
12
  s.email = [ "mail@wintoni.us" ]
@@ -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.request_uri)
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.request_uri, json.encode(cache))
60
- local res = ngx.location.capture(ngx.var.request_uri)
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.request_uri, json.encode(res))
79
+ memc:set(ngx.var.uri, json.encode(res))
80
80
  return debug("write cache")
81
81
  end)
82
82
  coroutine.resume(co)
@@ -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] = ttl if ttl
19
- data[:time] -= data[:ttl] || 10
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 = get_and_set_time(uri) || { :time => Time.now.to_i }
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
@@ -9,7 +9,7 @@ require "#{$root}/lib/accelerator"
9
9
 
10
10
  def request(wait=nil)
11
11
  sleep(wait) if wait
12
- `curl -s http://localhost:8080/test`.strip.to_i
12
+ `curl -s http://localhost:8080/test#{"?blah" if Random.rand(2) == 0}`.strip.to_i
13
13
  end
14
14
 
15
15
  def response_tests(time)
@@ -40,7 +40,7 @@ access = (opts) ->
40
40
  fn = ->
41
41
  memc = memclient(opts)
42
42
 
43
- cache, flags, err = memc\get(ngx.var.request_uri)
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.request_uri, json.encode(cache))
63
+ memc\set(ngx.var.uri, json.encode(cache))
64
64
 
65
65
  -- Make subrequest
66
- res = ngx.location.capture(ngx.var.request_uri)
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.request_uri, json.encode(res))
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.0
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