Implemented a basic block caching system.

- for block {block_id}, to have a cache with 3600 seconds of expiration,
    declare in the cms.ini
     [blocks]
     {block_id}.expiration=3600

Added support for size in feed aggregation with new field "size"
This commit is contained in:
2015-10-09 19:38:57 +02:00
parent 463105f29f
commit dffd06e331
11 changed files with 340 additions and 125 deletions

View File

@@ -141,6 +141,8 @@ feature -- Hook: block
local
bl: READABLE_STRING_8
bl_optional: BOOLEAN
l_ok: BOOLEAN
l_block_cache: detachable TUPLE [block: CMS_CACHE_BLOCK; region: READABLE_STRING_8; expired: BOOLEAN]
do
if attached subscribers ({CMS_HOOK_BLOCK}) as lst then
across
@@ -150,15 +152,24 @@ feature -- Hook: block
across
h.block_list as blst
loop
l_ok := False
bl := blst.item
bl_optional := bl.count > 0 and bl[1] = '?'
if bl_optional then
bl := bl.substring (2, bl.count)
if a_response.is_block_included (bl, False) then
h.get_block_view (bl, a_response)
l_ok := True
end
else
h.get_block_view (bl, a_response)
l_ok := True
end
if l_ok then
l_block_cache := a_response.block_cache (bl)
if l_block_cache /= Void and then not l_block_cache.expired then
a_response.add_block (l_block_cache.block, l_block_cache.region)
else
h.get_block_view (bl, a_response)
end
end
end
end

View File

@@ -0,0 +1,99 @@
note
description: "[
CMS_BLOCK implemented with a `cache'
as caching solution.
]"
date: "$Date: 2014-11-18 10:13:13 +0100 (mar., 18 nov. 2014) $"
revision: "$Revision: 96110 $"
class
CMS_CACHE_BLOCK
inherit
CMS_BLOCK
create
make
feature {NONE} -- Initialization
make (a_name: like name; a_cache: like cache)
require
a_name_not_blank: not a_name.is_whitespace
do
is_enabled := True
name := a_name
cache := a_cache
set_is_raw (True)
end
feature -- Access
name: READABLE_STRING_8
-- <Precursor>
title: detachable READABLE_STRING_32
-- <Precursor>
cache: CMS_CACHE [READABLE_STRING_8]
-- Cache content.
feature -- Status report
is_empty: BOOLEAN
-- Is current block empty?
do
Result := is_raw and not cache.exists
end
is_raw: BOOLEAN assign set_is_raw
-- Is raw?
-- If True, do not get wrapped it with block specific div
feature -- Element change
set_is_raw (b: BOOLEAN)
do
is_raw := b
end
set_name (n: like name)
-- Set `name' to `n'.
require
not n.is_whitespace
do
name := n
end
set_title (a_title: like title)
-- Set `title' to `a_title'.
do
title := a_title
end
feature -- Conversion
to_html (a_theme: CMS_THEME): STRING_8
do
debug
print ("REUSE CACHE for [" + name + "]!!!%N")
end
-- Why in this particular case theme is not used to generate the content?
if attached cache.item as l_content then
Result := l_content
else
Result := ""
check exists: False end
end
end
note
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -453,6 +453,29 @@ feature -- Blocks initialization
end
end
block_cache (a_block_id: READABLE_STRING_8): detachable TUPLE [block: CMS_CACHE_BLOCK; region: READABLE_STRING_8; expired: BOOLEAN]
-- Cached version of block `a_block_id'.
local
l_cache: CMS_FILE_STRING_8_CACHE
do
if
attached setup.text_item ("blocks." + a_block_id + ".expiration") as nb_secs and then
nb_secs.is_integer
then
if attached block_region_preference (a_block_id, "none") as l_region and then not l_region.same_string_general ("none") then
create l_cache.make (api.files_location.extended (".cache").extended ("blocks").extended (a_block_id).appended_with_extension ("html"))
if
l_cache.exists and then
not l_cache.expired (Void, nb_secs.to_integer)
then
Result := [create {CMS_CACHE_BLOCK} .make (a_block_id, l_cache), l_region, False]
else
Result := [create {CMS_CACHE_BLOCK} .make (a_block_id, l_cache), l_region, True]
end
end
end
end
feature -- Blocks regions
regions: STRING_TABLE [CMS_BLOCK_REGION]
@@ -786,6 +809,7 @@ feature -- Generation
l_region: CMS_BLOCK_REGION
l_menu_list_prepared: ARRAYED_LIST [CMS_LINK_COMPOSITE]
l_empty_blocks: detachable ARRAYED_LIST [CMS_BLOCK]
l_block_html: STRING
do
-- Menu
create {CMS_LOCAL_LINK} lnk.make ("Home", "")
@@ -884,7 +908,13 @@ feature -- Generation
end
end
end
page.add_to_region (theme.block_html (ic.item), reg_ic.item.name)
l_block_html := theme.block_html (ic.item)
if attached {CMS_CACHE_BLOCK} ic.item then
elseif attached block_cache (ic.item.name) as l_cache_block then
l_cache_block.block.cache.put (l_block_html)
end
page.add_to_region (l_block_html, reg_ic.item.name)
end
end