Python django_redis.get_redis_connection() Examples
The following are 30
code examples of django_redis.get_redis_connection().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django_redis
, or try the search function
.
Example #1
Source File: scheduled_tasks.py From cccatalog-api with MIT License | 6 votes |
def _save_views_to_db(view_keys, evict_from_cache=False): if not view_keys: return redis = get_redis_connection('traffic_stats') view_keys = [x.decode('utf-8') for x in view_keys] for obj in view_keys: model_name, model_id = obj.split(':') if model_name in model_name_to_instance: model = model_name_to_instance[model_name] try: instance = model.objects.get(id=model_id) instance.view_count = redis.get(obj) instance.save(update_fields=['view_count']) except ObjectDoesNotExist: log.warning('Tried to save views of non-existent instance.') else: log.warning( 'Tried to persist views of non-existent model ' + model_name ) if evict_from_cache: redis.delete(*view_keys) log.info('Saved ' + str(view_keys))
Example #2
Source File: views.py From eoj3 with MIT License | 6 votes |
def get_context_data(self, **kwargs): # pylint: disable=arguments-differ data = super(ServerList, self).get_context_data(**kwargs) redis_server = get_redis_connection("judge") sem = Semaphore(redis_server) sem.exists_or_init() try: data['semaphore_available_count'] = sem.available_count data['semaphore_available_keys'] = redis_server.lrange(sem.available_key, 0, sem.available_count) # 1 more actually data['semaphore_available_keys'] = list(map(lambda s: s.decode(), data['semaphore_available_keys'])) data['semaphore_grabbed_keys'] = {} for key, tt in redis_server.hgetall(sem.grabbed_key).items(): data['semaphore_grabbed_keys'][key.decode()] = sem.current_time - float(tt.decode()) data['server_synchronize_status_detail'] = cache.get('server_synchronize_status_detail', '') data['server_synchronize_status'] = cache.get('server_synchronize_status', 0) data['semaphore_ok'] = True except: pass data['crashed_submission_count'] = Submission.objects.filter(status=SubmissionStatus.SYSTEM_ERROR).count() return data
Example #3
Source File: api_test.py From micromasters with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_freeze_user_final_grade_error3(self, raise_on_exception, mock_refr, mock_get_fg): """ Test for freeze_user_final_grade function in case of problems with getting the final grade """ mock_get_fg.side_effect = AttributeError if not raise_on_exception: final_grade = api.freeze_user_final_grade(self.user, self.run_fa, raise_on_exception=raise_on_exception) assert final_grade is None else: with self.assertRaises(FreezeGradeFailedException): api.freeze_user_final_grade(self.user, self.run_fa, raise_on_exception=raise_on_exception) mock_refr.assert_called_once_with(self.user) mock_get_fg.assert_called_once_with(self.user, self.run_fa) assert FinalGrade.objects.filter(user=self.user, course_run=self.run_fa).exists() is False con = get_redis_connection("redis") failed_users_cache_key = api.CACHE_KEY_FAILED_USERS_BASE_STR.format(self.run_fa.edx_course_key) failed_users_count = con.llen(failed_users_cache_key) failed_users_list = list(map(int, con.lrange(failed_users_cache_key, 0, failed_users_count))) assert self.user.id in failed_users_list
Example #4
Source File: api_test.py From micromasters with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_freeze_user_final_grade_error2(self, raise_on_exception, mock_refr, mock_get_fg): """ Test for freeze_user_final_grade function in case of problems with refresh of cache """ mock_refr.side_effect = AttributeError if not raise_on_exception: final_grade = api.freeze_user_final_grade(self.user, self.run_fa, raise_on_exception=raise_on_exception) assert final_grade is None else: with self.assertRaises(FreezeGradeFailedException): api.freeze_user_final_grade(self.user, self.run_fa, raise_on_exception=raise_on_exception) assert mock_get_fg.called is False mock_refr.assert_called_once_with(self.user) assert FinalGrade.objects.filter(user=self.user, course_run=self.run_fa).exists() is False con = get_redis_connection("redis") failed_users_cache_key = api.CACHE_KEY_FAILED_USERS_BASE_STR.format(self.run_fa.edx_course_key) failed_users_count = con.llen(failed_users_cache_key) failed_users_list = list(map(int, con.lrange(failed_users_cache_key, 0, failed_users_count))) assert self.user.id in failed_users_list
Example #5
Source File: test_db.py From casepro with BSD 3-Clause "New" or "Revised" License | 6 votes |
def create_clean(self): """ Creates a clean database """ self._log("Generating database...\n") try: has_data = Org.objects.exists() except Exception: # pragma: no cover raise CommandError("Run migrate command first to create database tables") if has_data: raise CommandError("Can't clean database over non-empty database. Did you mean to use --resume?") # this is a new database so clear out redis self._log("Clearing out Redis cache... ") r = get_redis_connection() r.flushdb() self._log(self.style.SUCCESS("OK") + "\n") superuser = User.objects.create_superuser("root", "root@nyaruka.com", USER_PASSWORD) orgs = self.create_orgs(superuser, ORGS, GROUPS, FIELDS, PARTNERS, USER_PASSWORD) self.create_contacts(orgs, 100) return orgs
Example #6
Source File: api.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def save_cache_update_failure(user_id): """ Store the number of time update cache failed for a user Args: user_id (int): The user id """ con = get_redis_connection("redis") user_key = FIELD_USER_ID_BASE_STR.format(user_id) new_value = con.hincrby(CACHE_KEY_FAILURE_NUMS_BY_USER, user_key, 1) if int(new_value) >= 3: con.sadd(CACHE_KEY_FAILED_USERS_NOT_TO_UPDATE, user_id)
Example #7
Source File: scheduled_tasks.py From cccatalog-api with MIT License | 5 votes |
def do(self): log.info('Starting view count persistence job') redis = get_redis_connection('traffic_stats') one_day_ago = time.time() - 60 * 60 * 24 last_save_time = time.time() - (self.RUN_EVERY_MINS * 60) old_view_data = redis.zrangebyscore( 'model-last-accessed', '-inf', one_day_ago ) recent_view_data = redis.zrangebyscore( 'model-last-accessed', last_save_time, 'inf' ) self._save_views_to_db(old_view_data, evict_from_cache=True) redis.zremrangebyscore('model-last-accessed', '-inf', one_day_ago) self._save_views_to_db(recent_view_data) log.info('Saved cached traffic stats')
Example #8
Source File: views.py From ecommerce_website_development with BSD 2-Clause "Simplified" License | 5 votes |
def post(self, request): # 判断用户是否登录 user = request.user if not user.is_authenticated(): return JsonResponse({'res': 0, 'errmsg': '请先登录'}) # 接收参数 sku_id = request.POST.get('sku_id') # 参数校验 if not all([sku_id]): return JsonResponse({'res': 1, 'errmsg': '参数不完整'}) # 校验商品id requests urllib try: sku = GoodsSKU.objects.get(id=sku_id) except GoodsSKU.DoesNotExist: return JsonResponse({'res': 2, 'errmsg': '商品信息错误'}) # 业务处理: 删除用户的购物车记录 # 获取链接 conn = get_redis_connection('default') # 拼接key cart_key = 'cart_%d' % user.id # 删除记录 # hdel(key, *fields) conn.hdel(cart_key, sku_id) # 返回应答 return JsonResponse({'res': 3, 'errmsg': '删除购物车记录成功'})
Example #9
Source File: instance_database.py From Archery with Apache License 2.0 | 5 votes |
def create(request): """创建数据库""" instance_id = request.POST.get('instance_id', 0) db_name = request.POST.get('db_name') owner = request.POST.get('owner', '') remark = request.POST.get('remark', '') if not all([db_name]): return JsonResponse({'status': 1, 'msg': '参数不完整,请确认后提交', 'data': []}) try: instance = user_instances(request.user, db_type=['mysql']).get(id=instance_id) except Instance.DoesNotExist: return JsonResponse({'status': 1, 'msg': '你所在组未关联该实例', 'data': []}) try: owner_display = Users.objects.get(username=owner).display except Users.DoesNotExist: return JsonResponse({'status': 1, 'msg': '负责人不存在', 'data': []}) engine = get_engine(instance=instance) exec_result = engine.execute(db_name='information_schema', sql=f"create database {db_name};") if exec_result.error: return JsonResponse({'status': 1, 'msg': exec_result.error}) # 保存到数据库 else: InstanceDatabase.objects.create( instance=instance, db_name=db_name, owner=owner, owner_display=owner_display, remark=remark) # 清空实例资源缓存 r = get_redis_connection("default") for key in r.scan_iter(match='*insRes*', count=2000): r.delete(key) return JsonResponse({'status': 0, 'msg': '', 'data': []})
Example #10
Source File: api_test.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def patched_redis_keys(mocker): """Patch redis cache keys""" mocker.patch("dashboard.api.CACHE_KEY_FAILED_USERS_NOT_TO_UPDATE", TEST_CACHE_KEY_USER_IDS_NOT_TO_UPDATE) mocker.patch("dashboard.api.CACHE_KEY_FAILURE_NUMS_BY_USER", TEST_CACHE_KEY_FAILURES_BY_USER) yield con = get_redis_connection("redis") con.delete(TEST_CACHE_KEY_FAILURES_BY_USER) con.delete(TEST_CACHE_KEY_USER_IDS_NOT_TO_UPDATE)
Example #11
Source File: api_test.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_calculate_exclude_users(users_without_with_cache, patched_redis_keys): """ Users in the 'failed update cache' set should be excluded """ needs_update, _ = users_without_with_cache expected = needs_update[1:] con = get_redis_connection('redis') con.sadd(TEST_CACHE_KEY_USER_IDS_NOT_TO_UPDATE, needs_update[0].id) assert sorted(api.calculate_users_to_refresh_in_bulk()) == sorted([user.id for user in expected])
Example #12
Source File: api_test.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_save_cache_update_failures(db, patched_redis_keys): """Count the number of failures and then add to the list to not try to update cache""" user = _make_fake_real_user() con = get_redis_connection("redis") user_key = FIELD_USER_ID_BASE_STR.format(user.id) save_cache_update_failure(user.id) assert int(con.hget(TEST_CACHE_KEY_FAILURES_BY_USER, user_key)) == 1 save_cache_update_failure(user.id) assert int(con.hget(TEST_CACHE_KEY_FAILURES_BY_USER, user_key)) == 2 save_cache_update_failure(user.id) assert int(con.hget(TEST_CACHE_KEY_FAILURES_BY_USER, user_key)) == 3 assert con.sismember(TEST_CACHE_KEY_USER_IDS_NOT_TO_UPDATE, user.id) is True
Example #13
Source File: api.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def calculate_users_to_refresh_in_bulk(): """ Calculate the set of user ids which would be updated when running a bulk update. This uses a 6 hour delta because this is a bulk operation. For individual updates see CachedEdxDataApi.is_cache_fresh. Returns: list of int: A list of user ids which need to be updated """ refresh_time_limit = now_in_utc() - datetime.timedelta(hours=6) all_users = User.objects.filter(is_active=True, profile__fake_user=False).exclude(social_auth=None) con = get_redis_connection("redis") user_ids_invalid_credentials = con.smembers(CACHE_KEY_FAILED_USERS_NOT_TO_UPDATE) # If one of these fields is null in the database the gte expression will be false, so we will refresh those users users_not_expired = all_users.filter( usercacherefreshtime__enrollment__gte=refresh_time_limit, usercacherefreshtime__certificate__gte=refresh_time_limit, usercacherefreshtime__current_grade__gte=refresh_time_limit ) return list( all_users .exclude(id__in=users_not_expired.values_list("id", flat=True)) .exclude(id__in=user_ids_invalid_credentials) .values_list("id", flat=True) )
Example #14
Source File: models.py From casepro with BSD 3-Clause "New" or "Revised" License | 5 votes |
def lock(cls, org, uuid): return get_redis_connection().lock(GROUP_LOCK_KEY % (org.pk, uuid), timeout=60)
Example #15
Source File: views.py From micromasters with BSD 3-Clause "New" or "Revised" License | 5 votes |
def complete(request, *args, **kwargs): """Override this method so we can force user to be logged out.""" # This view overrides the behavior of the default 'complete' endpoint in order # to log out the user first. If user 1 is already logged in and user 2 is logged in on edX, # social_core can get confused on which User should get the SocialAuth object. if request.user.is_authenticated: key = "{}_state".format(request.backend.name) redirect_url = request.session.get("next") backend_state = request.session.get(key) logout(request) # logout will clear the session, this preserves the backend session state. We need to do # this so that this workflow will validate correctly (EdxOrgOAuth2.validate_state). # key is the same one used in EdxOrgAuth2.get_session_state(). request.session[key] = backend_state request.session["next"] = redirect_url # Continue with social_core pipeline social_complete_rtn = social_complete(request, *args, **kwargs) # Update redis cache if user had invalid credentials if request.user.is_authenticated: con = get_redis_connection("redis") user_key = FIELD_USER_ID_BASE_STR.format(request.user.id) con.hdel(CACHE_KEY_FAILURE_NUMS_BY_USER, user_key) con.srem(CACHE_KEY_FAILED_USERS_NOT_TO_UPDATE, request.user.id) return social_complete_rtn
Example #16
Source File: views.py From eoj3 with MIT License | 5 votes |
def post(self, request, pk): server = Server.objects.get(pk=pk) server.enabled = not server.enabled server.save(update_fields=['enabled']) try: Semaphore(get_redis_connection("judge")).reset() except: pass return HttpResponseRedirect(reverse('backstage:server'))
Example #17
Source File: views.py From eoj3 with MIT License | 5 votes |
def post(self, request, *args, **kwargs): try: Semaphore(get_redis_connection("judge")).reset() except: pass return HttpResponseRedirect(reverse('backstage:server'))
Example #18
Source File: checks.py From python-dockerflow with Mozilla Public License 2.0 | 5 votes |
def check_redis_connected(app_configs, **kwargs): """ A Django check to connect to the default redis connection using ``django_redis.get_redis_connection`` and see if Redis responds to a ``PING`` command. """ import redis from django_redis import get_redis_connection errors = [] try: connection = get_redis_connection("default") except redis.ConnectionError as e: msg = "Could not connect to redis: {!s}".format(e) errors.append(checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_REDIS)) except NotImplementedError as e: msg = "Redis client not available: {!s}".format(e) errors.append(checks.Error(msg, id=health.ERROR_MISSING_REDIS_CLIENT)) except ImproperlyConfigured as e: msg = 'Redis misconfigured: "{!s}"'.format(e) errors.append(checks.Error(msg, id=health.ERROR_MISCONFIGURED_REDIS)) else: result = connection.ping() if not result: msg = "Redis ping failed" errors.append(checks.Error(msg, id=health.ERROR_REDIS_PING_FAILED)) return errors
Example #19
Source File: mixins.py From django-test-addons with MIT License | 5 votes |
def setUpClass(cls): try: from django_redis import get_redis_connection cls.redis_connections = [get_redis_connection(connection_name) for connection_name in list(settings.CACHES.keys())] except ImportError as exc: raise ImportError("django_redis must be installed to use RedisTestCase. Exception details:- {0}".format(repr(exc))) except AttributeError as exc: raise AttributeError("settings file doesn't have redis configuration defined. Define CACHES in test settings file. Exception details:- {0}".format(repr(exc))) super(RedisTestMixin, cls).setUpClass()
Example #20
Source File: models.py From casepro with BSD 3-Clause "New" or "Revised" License | 5 votes |
def lock(cls, org, key): return get_redis_connection().lock(FIELD_LOCK_KEY % (org.pk, key), timeout=60)
Example #21
Source File: models.py From casepro with BSD 3-Clause "New" or "Revised" License | 5 votes |
def lock(cls, org, uuid): return get_redis_connection().lock(CONTACT_LOCK_KEY % (org.pk, uuid), timeout=60)
Example #22
Source File: models.py From casepro with BSD 3-Clause "New" or "Revised" License | 5 votes |
def lock(cls, org, uuid): return get_redis_connection().lock(LABEL_LOCK_KEY % (org.pk, uuid), timeout=60)
Example #23
Source File: models.py From casepro with BSD 3-Clause "New" or "Revised" License | 5 votes |
def lock(cls, org, backend_id): return get_redis_connection().lock(MESSAGE_LOCK_KEY % (org.pk, backend_id), timeout=60)
Example #24
Source File: redis_broker.py From django-q with MIT License | 5 votes |
def get_connection(list_key: str = Conf.PREFIX) -> Redis: if django_redis and Conf.DJANGO_REDIS: return django_redis.get_redis_connection(Conf.DJANGO_REDIS) if isinstance(Conf.REDIS, str): return redis.from_url(Conf.REDIS) return redis.StrictRedis(**Conf.REDIS)
Example #25
Source File: logger.py From cornerwise with MIT License | 5 votes |
def __init__(self, name="default", topic="default", nrecents=1000, expiration=86400, **kwargs): super().__init__(**kwargs) self.redis = get_redis_connection(name) self.topic = topic self.nrecents = nrecents self.expiration = expiration
Example #26
Source File: storage.py From lego with MIT License | 5 votes |
def redis(self): try: return self._redis except AttributeError: self._redis = get_redis_connection("default") return self._redis
Example #27
Source File: view_count.py From cccatalog-api with MIT License | 5 votes |
def _is_recent_visitor(ip, object_key): """ Return True if a given `ip` was seen accessing an `object_key` in the last 10 hours. Cleans out old IPs. :return: bool """ redis = get_redis_connection('traffic_stats') recent_ips_key = object_key + '-recent-ips' # Delete all IPs that haven't visited the resource in the last 10 hours. ten_hours_ago = time.time() - (60 * 60 * 10) redis.zremrangebyscore(recent_ips_key, '-inf', ten_hours_ago) is_recent = bool(redis.zscore(recent_ips_key, ip)) return is_recent
Example #28
Source File: flush_cache.py From zing with GNU General Public License v3.0 | 5 votes |
def handle(self, **options): if ( not options["flush_stats"] and not options["flush_rqdata"] and not options["flush_django_cache"] and not options["flush_all"] ): raise CommandError( "No options were provided. Use one of " "--django-cache, --rqdata, --stats or --all." ) self.stdout.write("Flushing cache...") if options["flush_stats"] or options["flush_all"]: # Delete all stats cache data. r_con = get_redis_connection("stats") r_con.flushdb() self.stdout.write("All stats data removed.") if options["flush_rqdata"] or options["flush_all"]: # Flush all rq data, dirty counter and restore Pootle revision # value. r_con = get_redis_connection("redis") r_con.flushdb() self.stdout.write("RQ data removed.") Revision.set(Unit.max_revision()) self.stdout.write("Max unit revision restored.") if options["flush_django_cache"] or options["flush_all"]: r_con = get_redis_connection("default") r_con.flushdb() self.stdout.write("All default Django cache data removed.")
Example #29
Source File: throttle.py From cccatalog-api with MIT License | 5 votes |
def _from_internal_network(ip): redis = get_redis_connection('default') return redis.sismember('ip-whitelist', ip)
Example #30
Source File: dead_link_mask.py From cccatalog-api with MIT License | 5 votes |
def get_query_mask(query_hash: str) -> List[int]: """ Fetches an existing query mask for a given query hash or returns an empty one. :param query_hash: Unique value for a particular query. :return: Boolean mask as a list of integers (0 or 1). """ redis = get_redis_connection("default") key = f'{query_hash}:dead_link_mask' return list(map(int, redis.lrange(key, 0, -1)))