@@ -104,18 +104,22 @@ def many_from_cache(self, preferences):
104
104
if k in cached
105
105
}
106
106
107
- def to_cache (self , pref ):
107
+ def to_cache (self , * prefs ):
108
108
"""
109
- Update/create the cache value for the given preference model instance
109
+ Update/create the cache value for the given preference model instances
110
110
"""
111
- key = self .get_cache_key (pref .section , pref .name )
112
- value = pref .raw_value
113
- if value is None or value == "" :
114
- # some cache backends refuse to cache None or empty values
115
- # resulting in more DB queries, so we cache an arbitrary value
116
- # to ensure the cache is hot (even with empty values)
117
- value = preferences_settings .CACHE_NONE_VALUE
118
- self .cache .set (key , value )
111
+ update_dict = {}
112
+ for pref in prefs :
113
+ key = self .get_cache_key (pref .section , pref .name )
114
+ value = pref .raw_value
115
+ if value is None or value == "" :
116
+ # some cache backends refuse to cache None or empty values
117
+ # resulting in more DB queries, so we cache an arbitrary value
118
+ # to ensure the cache is hot (even with empty values)
119
+ value = preferences_settings .CACHE_NONE_VALUE
120
+ update_dict [key ] = value
121
+
122
+ self .cache .set_many (update_dict )
119
123
120
124
def pref_obj (self , section , name ):
121
125
return self .registry .get (section = section , name = name )
@@ -220,6 +224,8 @@ def load_from_db(self, cache=False):
220
224
"""Return a dictionary of preferences by section directly from DB"""
221
225
a = {}
222
226
db_prefs = {p .preference .identifier (): p for p in self .queryset }
227
+ cache_prefs = []
228
+
223
229
for preference in self .registry .preferences ():
224
230
try :
225
231
db_pref = db_prefs [preference .identifier ()]
@@ -232,8 +238,11 @@ def load_from_db(self, cache=False):
232
238
else :
233
239
# cache if create_db_pref() hasn't already done so
234
240
if cache :
235
- self . to_cache (db_pref )
241
+ cache_prefs . append (db_pref )
236
242
237
243
a [preference .identifier ()] = db_pref .value
238
244
245
+ if cache_prefs :
246
+ self .to_cache (* cache_prefs )
247
+
239
248
return a
0 commit comments