def iter_users(self, page=None, prefix=None, prefix_limit=None): self._cache_users() if page is not None: page_total = self.page_total page = min(page, page_total) if page == 0: page_index_s = 0 else: page_index_s = int(cache_db.dict_get(self.get_cache_key( 'users_page_index'), str(page))) if page == page_total: page_index_e = None else: page_index_e = int(cache_db.dict_get(self.get_cache_key( 'users_page_index'), str(page + 1))) for user_id in cache_db.list_iter_range( self.get_cache_key('users_sorted'), page_index_s, page_index_e): user = User.get_user(self, id=user_id) if user: yield user elif prefix is not None: users_dict = {} users_sort = [] prefix_count = 0 users_trie = CacheTrie(self.get_cache_key('users_trie')) for user_data in users_trie.iter_prefix(prefix): user_id, user_type, user_name = user_data.split('-', 2) if user_type == CERT_CLIENT: prefix_count += 1 name_id = user_name + '_' + user_id users_dict[name_id] = (user_id, user_type, user_name) users_sort.append(name_id) self._last_prefix_count = prefix_count user_count = 0 search_more = False for name_id in sorted(users_sort): user_id, user_type, user_name = users_dict[name_id] user = User.get_user(self, id=user_id) if not user: continue yield user if prefix_limit and user_type == CERT_CLIENT: user_count += 1 if user_count >= prefix_limit: search_more = True break if prefix_limit and not search_more: yield None else: for user_id in cache_db.list_iter( self.get_cache_key('users_sorted')): user = User.get_user(self, id=user_id) if user: yield user
def _remove_cache_trie_key(self): users_trie = CacheTrie(self.org.get_cache_key('users_trie')) users_trie.remove_key(self.name, '%s-%s-%s' % ( self.id, self.type, self.name))
def _add_cache_trie_key(self): users_trie = CacheTrie(self.org.get_cache_key('users_trie')) users_trie.add_key_terms(self.name, '%s-%s-%s' % ( self.id, self.type, self.name))