mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
cache doc word counts
This commit is contained in:
parent
95d4a36dbd
commit
dc05d7ece4
@ -7,6 +7,9 @@ class SaveDocumentRevisionJob < ApplicationJob
|
||||
document = Document.find(document_id)
|
||||
return unless document.present?
|
||||
|
||||
# Update cached word count for the document regardless of how often this is called
|
||||
document.update(cached_word_count: document.computed_word_count)
|
||||
|
||||
# Make sure we're only storing revisions at least every 5 min
|
||||
latest_revision = document.document_revisions.order('created_at DESC').limit(1).first
|
||||
if latest_revision.present? && latest_revision.created_at > 5.minutes.ago
|
||||
|
||||
@ -77,7 +77,10 @@ class Document < ApplicationRecord
|
||||
end
|
||||
|
||||
def word_count
|
||||
# TODO this should probably be cached !!
|
||||
self.cached_word_count || self.computed_word_count
|
||||
end
|
||||
|
||||
def computed_word_count
|
||||
(self.body || "").split(/\s+/).count
|
||||
end
|
||||
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
class AddCachedWordCountToDocuments < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :documents, :cached_word_count, :integer
|
||||
end
|
||||
end
|
||||
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_06_27_023405) do
|
||||
ActiveRecord::Schema.define(version: 2021_07_01_002938) do
|
||||
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
@ -1190,6 +1190,7 @@ ActiveRecord::Schema.define(version: 2021_06_27_023405) do
|
||||
t.boolean "favorite"
|
||||
t.text "notes_text"
|
||||
t.integer "folder_id"
|
||||
t.integer "cached_word_count"
|
||||
t.index ["folder_id"], name: "index_documents_on_folder_id"
|
||||
t.index ["universe_id", "deleted_at"], name: "index_documents_on_universe_id_and_deleted_at"
|
||||
t.index ["universe_id"], name: "index_documents_on_universe_id"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user