Skip to content
Permalink
Browse files

Save index list to disk for faster start

Finally. Should have done this years ago, but have been busy.i What can
you do?!

Fixes: #102
Fixes: #96
References: #98
  • Loading branch information...
atomicules committed Nov 5, 2018
1 parent 0ca086e commit a5611663362dbbd22ae9be504de04032a92c2a3e
Showing with 24 additions and 1 deletion.
  1. +3 −1 README.md
  2. +21 −0 autoload/SimplenoteUtilities.py
@@ -76,7 +76,9 @@ navigate through the with the arrow keys and enter a note on hitting `Return`.

If you want to refresh the list index just run `:SimplenoteList` again. With
version 2.1 an in-memory cache is used to speed up subsequent updates of the
list index (the first load can still be slow if you have a lot of notes). Also
list index. With 2.2 this is extended so any call to `:SimplenoteList` also
updates an on-disk cache so startup is also fast (the very first load before
you have a cache can still be slow if you have a lot of notes). Also
see [Single Window Mode](#single-window-mode).

Now that you see the content of the note, you can interact with this specific
@@ -1,5 +1,6 @@
# Python classes and methods for simplenote.vim

import json
import os
import sys
import vim
@@ -16,8 +17,10 @@
from queue import Queue
else:
from Queue import Queue
import os.path

DEFAULT_SCRATCH_NAME = vim.eval("g:simplenote_scratch_buffer")
INDEX_CACHE_FILE = os.path.join(os.path.expanduser("~"),".snvim")

class SimplenoteVimInterface(object):
""" Interface class to provide functions for interacting with VIM """
@@ -28,6 +31,14 @@ def __init__(self, username, password):
self.note_index = []
# Lightweight "cache" of note data for note index
self.note_cache = {}
if os.path.isfile(INDEX_CACHE_FILE):
try:
with open(INDEX_CACHE_FILE, 'r') as f:
cache_file = json.load(f)
self.note_cache = cache_file["cache"]
self.simplenote.current = cache_file["current"]
except IOError as e:
print("Error: Unable to read index cache to file - %s" % e)
# TODO: Maybe possible to merge the following with note_cache now?
self.note_version = {}
# Map bufnums to noteids
@@ -529,6 +540,7 @@ def delete_current_note(self):
del self.bufnum_to_noteid[vim.current.buffer.number]
# Also need to remove from our cache
del self.note_cache[note_id]
self.write_index_cache()
vim.command("bdelete!")
else:
print("Deleting note failed.: %s" % note)
@@ -688,6 +700,8 @@ def list_note_index_in_scratch_buffer(self, tags=[]):
else:
note_keys, status = self.simplenote.get_note_list(data=False)
self.note_cache = self.get_notes_from_keys([n['key'] for n in note_keys])
# Write out cache
self.write_index_cache()
note_list = list(self.note_cache.values())

if (len(tags) > 0):
@@ -696,6 +710,7 @@ def list_note_index_in_scratch_buffer(self, tags=[]):
else:
note_list = [n for n in note_list if n["deleted"] != 1]


# set global notes index object to notes
if status == 0:
note_titles = []
@@ -730,6 +745,12 @@ def list_note_index_in_scratch_buffer(self, tags=[]):
vim.command("nnoremap <buffer><silent> <CR> <Esc>:call <SID>GetNoteToCurrentBuffer()<CR>")
vim.command("setlocal filetype=simplenote")

def write_index_cache(self):
try:
with open(INDEX_CACHE_FILE, 'w') as f:
json.dump({ "current": self.simplenote.current, "cache": self.note_cache}, f, indent=2)
except IOError as e:
print("Error: Unable to write index cache to file - %s" % e)

def get_note_title(note):
""" get title of note """

0 comments on commit a561166

Please sign in to comment.
You can’t perform that action at this time.