File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change 1
1
from django .http import HttpResponseForbidden
2
2
from django .conf import settings
3
+ from django .core .cache import cache
3
4
4
5
from models import BlockIP
5
6
@@ -22,7 +23,11 @@ def process_request(self, request):
22
23
ip = get_ip (request )
23
24
# TODO: Look into something more optimized for large numbers
24
25
# of blocks. https://github.com/jimfunk/django-postgresql-netfields
25
- deny_ips = [i .get_network () for i in BlockIP .objects .all ()]
26
+ block_ips = cache .get ('blockip:list' )
27
+ if block_ips is None :
28
+ block_ips = BlockIP .objects .all ()
29
+ cache .set ('blockip:list' , block_ips )
30
+ deny_ips = [i .get_network () for i in block_ips ]
26
31
27
32
for net in deny_ips :
28
33
if ip in net :
Original file line number Diff line number Diff line change 2
2
3
3
from django .db import models
4
4
from django .utils .translation import ugettext_lazy as _
5
+ from django .core .cache import cache
6
+ from django .db .models .signals import post_save , post_delete
5
7
6
8
7
9
class BlockIP (models .Model ):
@@ -17,3 +19,11 @@ def get_network(self):
17
19
class Meta :
18
20
verbose_name = _ ('IPs & masks to ban' )
19
21
verbose_name_plural = _ ('IPs & masks to ban' )
22
+
23
+
24
+ def _clear_cache (sender , instance , ** kwargs ):
25
+ cache .set ('blockip:list' , BlockIP .objects .all ())
26
+
27
+
28
+ post_save .connect (_clear_cache , sender = BlockIP )
29
+ post_delete .connect (_clear_cache , sender = BlockIP )
You can’t perform that action at this time.
0 commit comments