-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.rb
More file actions
56 lines (47 loc) · 1.4 KB
/
Copy pathmodels.rb
File metadata and controls
56 lines (47 loc) · 1.4 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
require 'dm-core'
require 'appengine-apis/datastore'
require 'appengine-apis/memcache'
DataMapper.setup(:default, "appengine://auto")
NUM_SHARDS = 20
class Counter
class << self
def increment(name)
shard_num = rand(NUM_SHARDS)
shard_name = "#{name}#{shard_num}"
shard_key = AppEngine::Datastore::Key.from_path(self.to_s, shard_name)
AppEngine::Datastore.transaction do
begin
shard = AppEngine::Datastore.get shard_key
rescue AppEngine::Datastore::EntityNotFound
shard = AppEngine::Datastore::Entity.new(shard_key)
shard["count"] = 0
shard["counter"] = name
end
shard.set_property("count", shard["count"] + 1)
AppEngine::Datastore.put(shard)
end
memcache.incr(name)
end
def get_count(name)
unless count = memcache.get(name)
query = AppEngine::Datastore::Query.new(self.to_s)
query.filter("counter", "==", name)
count = query.iterator.inject(0) {|sum, e| sum += e["count"]}
memcache.set(name, count)
end
count
end
def stats
memcache.stats
end
def memcache
@memcache ||= AppEngine::Memcache.new(:namespace => self.to_s)
end
end
end
class UserInfo
include DataMapper::Resource
property :id, Serial
property :views, Integer, :default => 0
property :appengine_user_id, String, :required => true
end