tinycodinglm / sys /sys-0004.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 19)
034ba67 verified
|
Raw History Blame
2.31 kB
{"type": "session", "harness": "deepseek-harness", "id": "sys-0004", "name": "Redis caching patterns"}
{"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}}
{"type": "message", "message": {"role": "user", "content": "How do I use Redis for caching?"}}
{"type": "message", "message": {"role": "assistant", "content": "```python\nimport redis\nimport json\nfrom functools import wraps\n\nr = redis.Redis(host='localhost', port=6379, decode_responses=True)\n\n# Basic operations\nr.set('user:1', json.dumps({'name': 'Alice'}), ex=3600) # TTL 1 hour\nuser = json.loads(r.get('user:1'))\nr.delete('user:1')\n\n# Cache decorator\ndef cache(ttl=300):\n def decorator(func):\n @wraps(func)\n def wrapper(*args, **kwargs):\n key = f\"cache:{func.__name__}:{hash(str(args) + str(kwargs))}\"\n cached = r.get(key)\n if cached:\n return json.loads(cached)\n result = func(*args, **kwargs)\n r.setex(key, ttl, json.dumps(result))\n return result\n return wrapper\n return decorator\n\n@cache(ttl=600)\ndef get_user(user_id):\n return db.query(\"SELECT * FROM users WHERE id = %s\", user_id)\n\n# Cache-aside pattern\ndef get_product(product_id):\n key = f\"product:{product_id}\"\n cached = r.get(key)\n if cached:\n return json.loads(cached)\n product = db.get_product(product_id)\n r.setex(key, 300, json.dumps(product))\n return product\n\n# Rate limiting\ndef is_rate_limited(user_id, limit=100, window=60):\n key = f\"rate:{user_id}\"\n current = r.incr(key)\n if current == 1:\n r.expire(key, window)\n return current > limit\n\n# Pub/Sub\nr.publish('notifications', json.dumps({'event': 'order_created', 'id': 123}))\n\n# Sorted sets (leaderboards)\nr.zadd('leaderboard', {'Alice': 100, 'Bob': 85, 'Charlie': 95})\ntop10 = r.zrevrange('leaderboard', 0, 9, withscores=True)\n```"}}