def save_to_redis(key_values): redis_key = key_values["key"] redis_value = key_values["value"] type = key_values["type"] if type == "user_like": # story_key, user_id - user stored in the stories list check = r.sismember(redis_key, redis_value) print check if check == 0: r.sadd(redis_key, redis_value) #FIGURE OUT HOW TO RETURN COUNT OR DONT HAVE THIS TAKE AND ARRAY count = r.scard(redis_key) return True, count else: r.srem(redis_key, redis_value) count = r.scard(redis_key) return True, count elif type == "story_like": # user_key, story_id - story_id stored in user list # This needs to be story!!! check = r.sismember(redis_key, redis_value) if check == 0: r.sadd(redis_key, redis_value) count = r.scard(redis_key) return True, count else: r.srem(redis_key, redis_value) count = r.scard(redis_key) return True, count else: return False, 0
def fetch_detail(*listing_ids): """Fetches and stores detailed listing data.""" data = get_listing_data(*listing_ids) for listing in data: if listing_is_active(listing): listing["users"] = r.scard("listings.%s.users" % listing["listing_id"]) save_listing(listing) score_listing(listing) else: purge_data(listing["listing_id"])
def scrub_scrubs(): """Randomly culls single-user lists.""" scrub_limit = app.config.get("BT_SCRUB_LIMIT") users_keys = r.keys("listings.*.users") # Preserve at least 5000, scrubbing half the remainder scrub_count = max(len(users_keys) - scrub_limit, 0) / 2 for key in random.sample(users_keys, scrub_count): if r.scard(key) < 2: _, listing_id, _ = key.split(".") purge_data(listing_id)
def test_scrub_scrubs(self): """Should randomly cull user lists of one user.""" for i in range(50): r.sadd('listings.%s.users' % i, '1', '2') for i in range(50, 6000): r.sadd('listings.%s.users' % i, '1') scrub_scrubs() # All of the 2 or more lists should be there for i in range(50): assert r.scard('listings.%s.users' % i) == 2 # Should leave at least 5000, taking about half of the remainder remaining_keys = r.keys('listings.*.users') assert len(remaining_keys) <= 5550 assert len(remaining_keys) >= 5000