001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.pageStore;
018
019import org.apache.wicket.page.IManageablePage;
020
021/**
022 * A store of pages that uses an {@link IPageStore} as a cache in front of another store to delegate to.
023 */
024public class CachingPageStore extends DelegatingPageStore
025{
026
027        /**
028         * The cache.
029         */
030        private final IPageStore cache;
031
032        /**
033         * Constructor.
034         * @param delegate store to delegate to
035         * @param cache store to use as cache
036         */
037        public CachingPageStore(IPageStore delegate, IPageStore cache)
038        {
039                super(delegate);
040                
041                this.cache = cache;
042        }
043
044        /**
045         * Get the store used a cache.
046         * 
047         * @return store
048         */
049        public IPageStore getCache()
050        {
051                return cache;
052        }
053
054        /**
055         * Get the page from cache first.
056         */
057        @Override
058        public IManageablePage getPage(IPageContext context, int id)
059        {
060                IManageablePage page = cache.getPage(context, id);
061                if (page != null) {
062                        return page;
063                }
064                
065                return getDelegate().getPage(context, id);
066        }
067
068        @Override
069        public void addPage(IPageContext context, IManageablePage page)
070        {
071                cache.addPage(context, page);
072
073                getDelegate().addPage(context, page);
074        }
075
076        @Override
077        public void removePage(IPageContext context, IManageablePage page)
078        {
079                cache.removePage(context, page);
080                
081                getDelegate().removePage(context, page);
082        }
083
084        @Override
085        public void removeAllPages(IPageContext context)
086        {
087                cache.removeAllPages(context);
088
089                getDelegate().removeAllPages(context);
090        }
091        
092        @Override
093        public void revertPage(IPageContext context, IManageablePage page)
094        {
095                cache.revertPage(context, page);
096                
097                getDelegate().revertPage(context, page);
098        }
099        
100        @Override
101        public void detach(IPageContext context)
102        {
103                cache.detach(context);
104                
105                getDelegate().detach(context);
106        }
107
108        @Override
109        public void destroy()
110        {
111                cache.destroy();
112                
113                getDelegate().destroy();
114        }
115}