From 4d9467bac99d96550902dfca4cfaee84651b5345 Mon Sep 17 00:00:00 2001
From: Shay Nehmad <shay.nehmad@guardicore.com>
Date: Wed, 2 Oct 2019 09:54:47 +0300
Subject: [PATCH] Using `ring` as the primary caching library, no functools.

Lowers amount of deps
---
 monkey/monkey_island/cc/models/monkey.py |  6 ++----
 monkey/monkey_island/cc/utils.py         | 16 ++++++++--------
 monkey/monkey_island/requirements.txt    |  1 -
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py
index 286ca4774..5a916e550 100644
--- a/monkey/monkey_island/cc/models/monkey.py
+++ b/monkey/monkey_island/cc/models/monkey.py
@@ -89,16 +89,14 @@ class Monkey(Document):
 
     # TODO This is not a field therefore cache shouldn't be here
     @staticmethod
-    @ring.lru(coder=ring.coder.JsonCoder())
+    @ring.lru()
     def get_label_by_id(object_id):
-        print("Actually in get_label_by_id - not cached for {}".format(str(object_id)))
         current_monkey = Monkey.get_single_monkey_by_id(object_id)
         return Monkey.get_hostname_by_id(object_id) + " : " + current_monkey.ip_addresses[0]
 
     @staticmethod
-    @ring.lru(coder=ring.coder.JsonCoder())
+    @ring.lru()
     def get_hostname_by_id(object_id):
-        print("Actually in get_hostname_by_id - not cached for {}".format(str(object_id)))
         return Monkey.get_single_monkey_by_id(object_id).hostname
 
     def set_hostname(self, hostname):
diff --git a/monkey/monkey_island/cc/utils.py b/monkey/monkey_island/cc/utils.py
index 3e03d18b9..cf59ae7df 100644
--- a/monkey/monkey_island/cc/utils.py
+++ b/monkey/monkey_island/cc/utils.py
@@ -6,11 +6,7 @@ import array
 import struct
 import ipaddress
 from netifaces import interfaces, ifaddresses, AF_INET
-
-try:
-    from functools import lru_cache
-except ImportError:
-    from backports.functools_lru_cache import lru_cache
+from ring import lru
 
 __author__ = 'Barak'
 
@@ -56,8 +52,8 @@ else:
 # The local IP addresses list should not change often. Therefore, we can cache the result and never call this function
 # more than once. This stopgap measure is here since this function is called a lot of times during the report
 # generation.
-# This means that if the interfaces of the Island machines change, the Island process needs to be restarted.
-@lru_cache(maxsize=1)
+# This means that if the interfaces of the Island machine change, the Island process needs to be restarted.
+@lru(maxsize=1)
 def local_ip_addresses():
     ip_list = []
     for interface in interfaces():
@@ -66,7 +62,11 @@ def local_ip_addresses():
     return ip_list
 
 
-@lru_cache(maxsize=1)
+# The subnets list should not change often. Therefore, we can cache the result and never call this function
+# more than once. This stopgap measure is here since this function is called a lot of times during the report
+# generation.
+# This means that if the interfaces or subnets  of the Island machine change, the Island process needs to be restarted.
+@lru(maxsize=1)
 def get_subnets():
     subnets = []
     for interface in interfaces():
diff --git a/monkey/monkey_island/requirements.txt b/monkey/monkey_island/requirements.txt
index d9fa69f0a..ee66bb797 100644
--- a/monkey/monkey_island/requirements.txt
+++ b/monkey/monkey_island/requirements.txt
@@ -26,5 +26,4 @@ mongoengine
 mongomock
 requests
 dpath
-backports.functools-lru-cache
 ring