Skip to content

Commit 2cc640e

Browse files
committedJun 27, 2011
Move subreddit name search to its own CF.
Subreddit name search is used for recommending subreddits to users as they type a name on the submission page. Also adds a missing script that is needed to load up the subreddit names into cassandra. This script should be run occasionally (we do it once per day) to fill/update the search cache.
1 parent 352d29a commit 2cc640e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed
 

‎r2/r2/lib/subreddit_search.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
from r2.models import *
1+
from r2.models import Subreddit
2+
from r2.lib.memoize import memoize
3+
from r2.lib.db.operators import desc
24
from r2.lib import utils
5+
from r2.lib.db import tdb_cassandra
6+
from r2.lib.cache import CL_ONE
37

4-
from pylons import g
5-
6-
sr_prefix = 'sr_search_'
7-
8+
class SubredditsByPartialName(tdb_cassandra.View):
9+
_use_db = True
10+
_value_type = 'pickle'
11+
_use_new_ring = True
12+
_read_consistency_level = CL_ONE
813

914
def load_all_reddits():
1015
query_cache = {}
@@ -21,11 +26,17 @@ def load_all_reddits():
2126
if len(names) < 10:
2227
names.append(sr.name)
2328

24-
g.permacache.set_multi(query_cache, prefix = sr_prefix)
29+
for name_prefix, subreddits in query_cache.iteritems():
30+
SubredditsByPartialName._set_values(name_prefix, {'srs': subreddits})
2531

2632
def search_reddits(query):
2733
query = str(query.lower())
28-
return g.permacache.get(sr_prefix + query) or []
34+
35+
try:
36+
result = SubredditsByPartialName._byID(query)
37+
return result.srs
38+
except tdb_cassandra.NotFound:
39+
return []
2940

3041
@memoize('popular_searches', time = 3600)
3142
def popular_searches():

‎scripts/update_sr_names.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
cd ~/reddit/r2
4+
~/reddit/scripts/saferun.sh /tmp/update_sr_names.pid nice /usr/local/bin/paster --plugin=r2 run run.ini r2/lib/subreddit_search.py -c "load_all_reddits()"

0 commit comments

Comments
 (0)
Please sign in to comment.