mirror of
				https://github.com/TitanEmbeds/Titan.git
				synced 2025-11-04 07:47:10 +01:00 
			
		
		
		
	Initial Redislite implementation with limits
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -90,3 +90,5 @@ ENV/
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# Project specifc
 | 
					# Project specifc
 | 
				
			||||||
config.py
 | 
					config.py
 | 
				
			||||||
 | 
					redislite.db
 | 
				
			||||||
 | 
					redislite.db.settings
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ app.config['SQLALCHEMY_DATABASE_URI'] = config['database-uri']
 | 
				
			|||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # Suppress the warning/no need this on for now.
 | 
					app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # Suppress the warning/no need this on for now.
 | 
				
			||||||
app.config['RATELIMIT_HEADERS_ENABLED'] = True
 | 
					app.config['RATELIMIT_HEADERS_ENABLED'] = True
 | 
				
			||||||
app.config['SQLALCHEMY_POOL_RECYCLE'] = 250
 | 
					app.config['SQLALCHEMY_POOL_RECYCLE'] = 250
 | 
				
			||||||
 | 
					app.config['RATELIMIT_STORAGE_URL'] = 'redislite://redislite.db'
 | 
				
			||||||
app.secret_key = config['app-secret']
 | 
					app.secret_key = config['app-secret']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
db.init_app(app)
 | 
					db.init_app(app)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,3 +5,4 @@ from guilds import Guilds
 | 
				
			|||||||
from unauthenticated_users import UnauthenticatedUsers
 | 
					from unauthenticated_users import UnauthenticatedUsers
 | 
				
			||||||
from unauthenticated_bans import UnauthenticatedBans
 | 
					from unauthenticated_bans import UnauthenticatedBans
 | 
				
			||||||
from authenticated_users import AuthenticatedUsers
 | 
					from authenticated_users import AuthenticatedUsers
 | 
				
			||||||
 | 
					from custom_redislite import LimitsRedisLite
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										25
									
								
								titanembeds/database/custom_redislite.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								titanembeds/database/custom_redislite.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					import urlparse
 | 
				
			||||||
 | 
					from limits.storage import Storage
 | 
				
			||||||
 | 
					from redislite import Redis
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class LimitsRedisLite(Storage): # For Python Limits
 | 
				
			||||||
 | 
					    STORAGE_SCHEME = "redislite"
 | 
				
			||||||
 | 
					    def __init__(self, uri, **options):
 | 
				
			||||||
 | 
					        self.redis_instance = Redis(urlparse.urlparse(uri).netloc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def check(self):
 | 
				
			||||||
 | 
					        return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_expiry(self, key):
 | 
				
			||||||
 | 
					        return self.redis_instance.ttl(key)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def incr(self, key, expiry, elastic_expiry=False):
 | 
				
			||||||
 | 
					        if self.redis_instance.exists(key):
 | 
				
			||||||
 | 
					            self.redis_instance.set(key, int(self.redis_instance.get(key))+1)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            self.redis_instance.set(key, 1)
 | 
				
			||||||
 | 
					        self.redis_instance.expire(key, expiry)
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get(self, key):
 | 
				
			||||||
 | 
					        return int(self.redis_instance.get(key))
 | 
				
			||||||
		Reference in New Issue
	
	Block a user