my-emacs-d/elpa/magithub-20161004.523/magithub-cache.el

89 lines
3.0 KiB
EmacsLisp
Raw Normal View History

2016-09-27 14:23:52 +00:00
;;; magithub-cache.el --- caching network data -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Sean Allred
;; Author: Sean Allred <code@seanallred.com>
;; Keywords: data, tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
2016-09-29 07:33:23 +00:00
(require 'magithub-core)
(defvar magithub-cache--cache ()
"An alist of hash tables to use as thr cache.
2016-09-27 14:23:52 +00:00
Entries are of the form (time-entered . value).")
(defvar magithub-cache-refresh-seconds-plist
2016-09-29 07:33:23 +00:00
(list :issues 600 :ci-status 60 t 60)
2016-09-27 14:23:52 +00:00
"The number of seconds that have to pass for GitHub data to be
considered outdated.")
2016-09-29 07:33:23 +00:00
(defun magithub-cache--get-table (repo)
(declare (indent defun))
(let ((table (cdr (assoc repo magithub-cache--cache))))
(unless table
(add-to-list 'magithub-cache--cache (cons repo (make-hash-table)))
(setq table (cdr (assoc repo magithub-cache--cache))))
table))
(defun magithub-cache--table (table cache default)
2016-09-27 14:23:52 +00:00
"The cached value for CACHE (set to DEFAULT if necessary)."
(declare (indent defun))
2016-09-29 07:33:23 +00:00
(let ((cached-value (gethash cache table :no-value)))
2016-09-27 14:23:52 +00:00
(if (or (eq cached-value :no-value)
2016-09-29 07:33:23 +00:00
(< (or (plist-get magithub-cache-refresh-seconds-plist cache)
(plist-get magithub-cache-refresh-seconds-plist t))
2016-09-27 14:23:52 +00:00
(time-to-seconds (time-since (car cached-value)))))
2016-09-29 07:33:23 +00:00
(cdr (puthash cache (cons (current-time) (eval default)) table))
2016-09-27 14:23:52 +00:00
(when magithub-debug-mode
(message "Using cached value for %S (retrieved %s)"
cache (format-time-string "%F %T" (car cached-value))))
(cdr cached-value))))
2016-09-29 07:33:23 +00:00
(defun magithub-cache (repo cache default)
(declare (indent defun))
(magithub-cache--table (magithub-cache--get-table repo) cache default))
(defun magithub-cache-value--table (table cache)
2016-09-27 14:23:52 +00:00
"The cached value for CACHE."
2016-09-29 07:33:23 +00:00
(let ((c (gethash cache table :no-value)))
2016-09-27 14:23:52 +00:00
(unless (eq c :no-value)
(cdr c))))
2016-09-29 07:33:23 +00:00
(defun magithub-cache-value (repo cache)
"The cached value for CACHE."
(magithub-cache-value--table (magithub-cache--get-table repo) cache))
(defun magithub-cache-clear (repo &optional cache)
"Clear the cache for CACHE.
If CACHE is nil, the entire cache is cleared."
(if cache (remhash cache (magithub-cache--get-table repo))
(cl-delete repo magithub-cache--cache
:key #'car :test #'equal)))
2016-09-27 14:23:52 +00:00
(defun magithub-refresh ()
"Refresh all GitHub data."
(interactive)
2016-09-29 07:33:23 +00:00
(magithub-cache-clear)
2016-09-27 14:23:52 +00:00
(magit-refresh))
(provide 'magithub-cache)
;;; magithub-cache.el ends here