@@ -15,6 +15,9 @@ package hugolib
1515
1616import (
1717 "path"
18+ "path/filepath"
19+
20+ "github.com/spf13/hugo/cache"
1821)
1922
2023// PageCollections contains the page collections for a site.
@@ -39,81 +42,70 @@ type PageCollections struct {
3942
4043 // Includes absolute all pages (of all types), including drafts etc.
4144 rawAllPages Pages
45+
46+ pageCache * cache.PartitionedLazyCache
4247}
4348
4449func (c * PageCollections ) refreshPageCaches () {
4550 c .indexPages = c .findPagesByKindNotIn (KindPage , c .Pages )
4651 c .RegularPages = c .findPagesByKindIn (KindPage , c .Pages )
4752 c .AllRegularPages = c .findPagesByKindIn (KindPage , c .AllPages )
48- }
49-
50- func newPageCollections () * PageCollections {
51- return & PageCollections {}
52- }
53-
54- func newPageCollectionsFromPages (pages Pages ) * PageCollections {
55- return & PageCollections {rawAllPages : pages }
56- }
57-
58- func (c * PageCollections ) getFirstPageMatchIn (pages Pages , typ string , pathElements ... string ) * Page {
5953
60- if len (pages ) == 0 {
61- return nil
62- }
63-
64- var filename string
65- if typ == KindPage {
66- filename = path .Join (pathElements ... )
67- }
68-
69- for _ , p := range pages {
70- if p .Kind != typ {
71- continue
72- }
73-
74- if typ == KindHome {
75- return p
76- }
77-
78- if typ == KindPage {
79- if p .Source .Path () == filename {
80- return p
54+ cacheLoader := func (kind string ) func () (map [string ]interface {}, error ) {
55+ return func () (map [string ]interface {}, error ) {
56+ cache := make (map [string ]interface {})
57+ switch kind {
58+ case KindPage :
59+ // Note that we deliberately use the pages from all sites
60+ // in this cache, as we intend to use this in the ref and relref
61+ // shortcodes. If the user says "sect/doc1.en.md", he/she knows
62+ // what he/she is looking for.
63+ for _ , p := range c .AllRegularPages {
64+ // TODO(bep) section
65+ cache [filepath .ToSlash (p .Source .Path ())] = p
66+ }
67+ default :
68+ for _ , p := range c .indexPages {
69+ key := path .Join (p .sections ... )
70+ cache [key ] = p
71+ }
8172 }
82- continue
83- }
8473
85- match := false
86- for i := 0 ; i < len (pathElements ); i ++ {
87- if len (p .sections ) > i && pathElements [i ] == p .sections [i ] {
88- match = true
89- } else {
90- match = false
91- break
92- }
93- }
94- if match {
95- return p
74+ return cache , nil
9675 }
9776 }
9877
99- return nil
78+ var partitions []cache. Partition
10079
101- }
80+ for _ , kind := range allKindsInPages {
81+ partitions = append (partitions , cache.Partition {Key : kind , Load : cacheLoader (kind )})
82+ }
10283
103- func (c * PageCollections ) getRegularPage (filename string ) {
84+ c .pageCache = cache .NewPartitionedLazyCache (partitions ... )
85+ }
10486
87+ func newPageCollections () * PageCollections {
88+ return & PageCollections {}
10589}
10690
107- func (c * PageCollections ) getPage (typ string , path ... string ) * Page {
108- var pages Pages
91+ func newPageCollectionsFromPages (pages Pages ) * PageCollections {
92+ return & PageCollections {rawAllPages : pages }
93+ }
10994
110- if typ == KindPage {
111- pages = c .AllPages
95+ func (c * PageCollections ) getPage (typ string , sections ... string ) * Page {
96+ var key string
97+ if len (sections ) == 1 {
98+ key = filepath .ToSlash (sections [0 ])
11299 } else {
113- pages = c . indexPages
100+ key = path . Join ( sections ... )
114101 }
115102
116- return c .getFirstPageMatchIn (pages , typ , path ... )
103+ // TODO(bep) section error
104+ p , _ := c .pageCache .Get (typ , key )
105+ if p == nil {
106+ return nil
107+ }
108+ return p .(* Page )
117109
118110}
119111
0 commit comments