def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] # ip should be like '115.23.65.112' # but on SAE the ip is like '115.23.65.112,115.23.65.112' # and that cause an cache error, so I split it. ip = ip.split(',')[0] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 整个逻辑很简单,每一个用户访问,我都会把用户的ip作为key放到memcache,然后有一个 online_ips 的key,用来存放所有的ip。 每次请求都会进行如下步骤,先取出 online_ips的所有值,然后再根据这个这个list来从memcache中取出依然存在的ip, 然后再次存入 online_ips 。 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT', []) if 'Spider' in http_user_agent or 'spider' in http_user_agent: return online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() ip = get_real_ip(request) cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)
def process_view(self, request, view_func, view_args, view_kwargs): """ 处理当前在线人数 """ http_user_agent = request.META.get('HTTP_USER_AGENT') if 'Spider' in http_user_agent or 'spider' in http_user_agent: return if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] online_ips = cache.get("online_ips", []) if online_ips: online_ips = cache.get_many(online_ips).keys() cache.set(ip, 0, 5 * 60) if ip not in online_ips: online_ips.append(ip) cache.set("online_ips", online_ips)