twitter-cache 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/twitter/cache/helpers.rb +8 -0
- data/lib/twitter/cache/redis.rb +3 -2
- data/lib/twitter/cache/version.rb +1 -1
- data/lib/twitter/cache/wrapper.rb +16 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53f52e4e5923bdf1373a5f48e309024330c11425
|
4
|
+
data.tar.gz: ee0236c79d3546b1c268825b88064aa9397145ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b440bddd79af310ee078309ea32d1c873bea26cceece3b626867d0360d956a6421b90a57eb3762c48742f9f919161d1c65fd0a366680c6a79586e39cabc476e
|
7
|
+
data.tar.gz: 46ca11e318e2c7e51ad040c6e0ce68a5e3827a25fa529ad6017bedcb7296ec6732c2c9e5c01e342d9b2f13508697e9c239545f1959d62591ab3bfdb07085d9b4
|
@@ -8,6 +8,14 @@ module Twitter
|
|
8
8
|
def cache
|
9
9
|
@cache ||= Twitter::Cache::Redis.new
|
10
10
|
end
|
11
|
+
|
12
|
+
def user_cache
|
13
|
+
@user_cache ||= Twitter::Cache::Redis.new(:user)
|
14
|
+
end
|
15
|
+
|
16
|
+
def friends_cache
|
17
|
+
@friends_cache ||= Twitter::Cache::Redis.new(:friends)
|
18
|
+
end
|
11
19
|
end
|
12
20
|
end
|
13
21
|
end
|
data/lib/twitter/cache/redis.rb
CHANGED
@@ -7,10 +7,11 @@ module Twitter
|
|
7
7
|
include Twitter::Cache::Helpers
|
8
8
|
attr_reader :client
|
9
9
|
|
10
|
-
def initialize
|
10
|
+
def initialize(prefix = nil)
|
11
11
|
redis_url = ENV['REDIS_URL'] || config.redis
|
12
12
|
redis = ::Redis.new(url: redis_url)
|
13
|
-
|
13
|
+
namespace = [config.namespace, prefix].compact.join(':')
|
14
|
+
@client = ::Redis::Namespace.new(namespace, redis: redis)
|
14
15
|
end
|
15
16
|
|
16
17
|
def method_missing(name, *args)
|
@@ -9,13 +9,13 @@ module Twitter
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def user(id = current_id)
|
12
|
-
|
12
|
+
user_cache.get(id, ttl: config.ttl) do
|
13
13
|
config.convert_user(twitter.user(id))
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def friend_ids(id = current_id)
|
18
|
-
|
18
|
+
friends_cache.get(id, ttl: config.ttl) do
|
19
19
|
twitter.friend_ids(id).to_a
|
20
20
|
end
|
21
21
|
end
|
@@ -23,18 +23,19 @@ module Twitter
|
|
23
23
|
def friends(id = current_id, per: 100, page: 0)
|
24
24
|
ids = friend_ids(id)
|
25
25
|
case page
|
26
|
-
when :rand, :random
|
27
|
-
users(ids.sample(per))
|
26
|
+
when :rand, :random, 'rand', 'random'
|
27
|
+
users(ids.sample(per.to_i))
|
28
28
|
else
|
29
|
-
start = per * page
|
29
|
+
start = per.to_i * page.to_i
|
30
30
|
users(ids[start, per])
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
def users(ids)
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
known_ids(ids).tap do |known_ids|
|
36
|
+
fetch_users(ids - known_ids) unless known_ids == ids
|
37
|
+
end
|
38
|
+
ids.map { |id| user_cache.get(id) }
|
38
39
|
end
|
39
40
|
|
40
41
|
def current_user
|
@@ -47,20 +48,15 @@ module Twitter
|
|
47
48
|
|
48
49
|
protected
|
49
50
|
|
50
|
-
def user_key(id)
|
51
|
-
"user:#{id}"
|
52
|
-
end
|
53
|
-
|
54
|
-
def friends_key(id)
|
55
|
-
"friends:#{id}"
|
56
|
-
end
|
57
|
-
|
58
51
|
def fetch_users(ids)
|
59
|
-
|
60
|
-
|
61
|
-
|
52
|
+
twitter.users(ids).each do |raw|
|
53
|
+
user = config.convert_user(raw)
|
54
|
+
user_cache.set(raw.id, user, ttl: config.ttl)
|
62
55
|
end
|
63
|
-
|
56
|
+
end
|
57
|
+
|
58
|
+
def known_ids(ids)
|
59
|
+
ids & user_cache.keys.map(&:to_i)
|
64
60
|
end
|
65
61
|
end
|
66
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twitter-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- masarakki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|