Skip to content

Commit

Permalink
Remove JSON (de)serialisation for SQL storage
Browse files Browse the repository at this point in the history
- Partially revert 764caec which added missing JSON (de)serialisation
- Data is already pickled and stored as a binary blob
- Add versioning via table name
- Bump to v2 to force cached data to be removed
- Old table(s) will be automatically removed
- Also remove row factory
  • Loading branch information
MoojMidge committed Dec 15, 2023
1 parent b209931 commit cb6b85a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 142 deletions.
5 changes: 2 additions & 3 deletions resources/lib/youtube_plugin/kodion/sql_store/data_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from __future__ import absolute_import, division, unicode_literals

import json
from datetime import datetime

from .storage import Storage
Expand Down Expand Up @@ -45,10 +44,10 @@ def get_item(self, content_id, seconds):
return None

current_time = datetime.now()
if self.get_seconds_diff(query_result[1] or current_time) > seconds:
if self.get_seconds_diff(query_result[0] or current_time) > seconds:
return None

return query_result[0]
return query_result[1]

def set_item(self, content_id, item):
self._set(content_id, item)
Expand Down
28 changes: 10 additions & 18 deletions resources/lib/youtube_plugin/kodion/sql_store/function_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
class FunctionCache(Storage):
def __init__(self, filename, max_file_size_mb=5):
max_file_size_kb = max_file_size_mb * 1024
super(FunctionCache, self).__init__(filename, max_file_size_kb=max_file_size_kb)
super(FunctionCache, self).__init__(filename,
max_file_size_kb=max_file_size_kb)

self._enabled = True

Expand Down Expand Up @@ -67,10 +68,9 @@ def get_cached_only(self, func, *args, **keywords):

# only return before cached data
data, cache_id = self._get_cached_data(partial_func)
if data is not None:
return data[0]

return None
if data is None:
return None
return data[1]

def get(self, func, seconds, *args, **keywords):
"""
Expand All @@ -86,22 +86,14 @@ def get(self, func, seconds, *args, **keywords):
if not self._enabled:
return partial_func()

cached_data = None
cached_time = None
data, cache_id = self._get_cached_data(partial_func)
if data is not None:
cached_data = data[0]
cached_time = data[1]

diff_seconds = 0

if cached_time is not None:
diff_seconds = self.get_seconds_diff(cached_time)

if cached_data is None or diff_seconds > seconds:
cached_data = partial_func()
self._set(cache_id, cached_data)
cached_time, cached_data = data

if data is None or self.get_seconds_diff(cached_time) > seconds:
data = partial_func()
self._set(cache_id, data)
return data
return cached_data

def _optimize_item_count(self):
Expand Down
29 changes: 6 additions & 23 deletions resources/lib/youtube_plugin/kodion/sql_store/playback_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,14 @@ def __init__(self, filename):
def is_empty(self):
return self._is_empty()

@staticmethod
def _process_item(item):
return item.strip('"').split(',')

def get_items(self, keys):
query_result = self._get_by_ids(keys, process=self._process_item)
query_result = self._get_by_ids(keys)
if not query_result:
return {}

result = {
item[0]: {
'play_count': int(item[2][0]),
'total_time': float(item[2][1]),
'played_time': float(item[2][2]),
'played_percent': int(item[2][3]),
'last_played': str(item[1]),
} for item in query_result
item[0]: dict(item[2], last_played=item[1])
for item in query_result
}
return result

Expand All @@ -44,14 +35,7 @@ def get_item(self, key):
if not query_result:
return {}

values = query_result[0].split(',')
result = {key: {
'play_count': int(values[0]),
'total_time': float(values[1]),
'played_time': float(values[2]),
'played_percent': int(values[3]),
'last_played': str(query_result[1]),
}}
result = {key: dict(query_result[1], last_played=query_result[0])}
return result

def clear(self):
Expand All @@ -60,9 +44,8 @@ def clear(self):
def remove(self, video_id):
self._remove(video_id)

def update(self, video_id, play_count, total_time, played_time, played_percent):
item = ','.join([str(play_count), str(total_time), str(played_time), str(played_percent)])
self._set(str(video_id), item)
def update(self, video_id, play_data):
self._set(video_id, play_data)

def _optimize_item_count(self):
pass
Expand Down
Loading

0 comments on commit cb6b85a

Please sign in to comment.