-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_damage.py
57 lines (48 loc) · 2 KB
/
retrieve_damage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pymysql
import redis
from mysqlcursor import cur, con
# Initialize Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def option_23():
"This is to retrieve all guns which have damage greater than a particular value"
'''
Take the following as input
Damage\n
'''
try:
Damage = input("Enter Damage: ")
# Create a unique cache key for this query
cache_key = f"weapons:damage_gt:{Damage}"
# Check if the result is cached in Redis
if redis_client.exists(cache_key):
print("Data found in cache:")
cached_data = redis_client.hgetall(cache_key)
print("Weapon_ID\tWeapon_Name\tAmmo\tFire_Rate\tDamage\tExtension_ID")
for key, value in cached_data.items():
print(value.decode('utf-8'))
else:
# SQL query to find weapons with damage greater than the specified value
query = """
SELECT Weapon_ID, Weapon_Name, Ammo, Fire_Rate, Damage, Extension_ID
FROM Weapons
WHERE Damage > %s
"""
cur.execute(query, (Damage,))
weapons = cur.fetchall()
if weapons:
print("Weapon_ID\tWeapon_Name\tAmmo\tFire_Rate\tDamage\tExtension_ID")
weapon_data = {}
for idx, row in enumerate(weapons):
weapon_str = f"{row['Weapon_ID']}\t{row['Weapon_Name']}\t{row['Ammo']}\t{row['Fire_Rate']}\t{row['Damage']}\t{row['Extension_ID']}"
print(weapon_str)
weapon_data[f"weapon_{idx}"] = weapon_str
# Cache the result in Redis
redis_client.hmset(cache_key, weapon_data)
print("Data cached in Redis")
else:
print("No weapons found with damage greater than", Damage)
con.commit()
except Exception as e:
con.rollback()
print("Failed to fetch from database")
print(">>>>>>>>>>>>>", e)