Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.webbasics.resource;
 
 import java.util.Map;
 
 
 
A global cache for WebSiteResource objects, to avoid the hash calculation over and over again.

Author(s):
Philip Helger
 
 public final class WebSiteResourceCache
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (WebSiteResourceCache.class);
   private static final ReadWriteLock s_aRWLock = new ReentrantReadWriteLock ();
   private static boolean s_bCacheEnabled = true;
   private static final Map <StringWebSiteResources_aMap = new HashMap <StringWebSiteResource> ();
 
   static
   {
     // Do it here to always have a small log output
     setCacheEnabled (!GlobalDebug.isDebugMode ());
   }
 
   @SuppressWarnings ("unused")
   private static final WebSiteResourceCache s_aInstance = new WebSiteResourceCache ();
 
   private WebSiteResourceCache ()
   {}

  

Returns:
true if the cache is globally enabled, false if the cache is disabled.
 
   public static boolean isCacheEnabled ()
   {
     .readLock ().lock ();
     try
     {
       return ;
     }
     finally
     {
       .readLock ().unlock ();
     }
   }

  
Globally enabled or disable the cache.

Parameters:
bCacheEnabled true to enable it, false to disable it.
 
   public static void setCacheEnabled (final boolean bCacheEnabled)
   {
     .writeLock ().lock ();
     try
     {
        = bCacheEnabled;
     }
     finally
     {
       .writeLock ().unlock ();
     }
     .info ("WebSiteResourceCache is now: " + (bCacheEnabled ? "enabled" : "disabled"));
   }
 
  public static WebSiteResource getOrCreateResource (@Nonnull final EWebSiteResourceType eResourceType,
                                                     @Nonnull @Nonempty final String sPath)
  {
    ValueEnforcer.notNull (eResourceType"ResourceType");
    ValueEnforcer.notEmpty (sPath"Path");
    if (!isCacheEnabled ())
    {
      // Always create a new resource to allow for modifications
      final WebSiteResource aResource = new WebSiteResource (eResourceTypesPath);
      if (!aResource.isExisting ())
        throw new IllegalArgumentException ("WebSiteResource '" +
                                            sPath +
                                            "' of type " +
                                            eResourceType +
                                            " does not exist");
      return aResource;
    }
    final String sCacheKey = eResourceType.getID () + "-" + sPath;
    // Entry already existing?
    WebSiteResource ret;
    .readLock ().lock ();
    try
    {
      ret = .get (sCacheKey);
    }
    finally
    {
      .readLock ().unlock ();
    }
    if (ret == null)
    {
      .writeLock ().lock ();
      try
      {
        // Try again in write lock
        ret = .get (sCacheKey);
        if (ret == null)
        {
          ret = new WebSiteResource (eResourceTypesPath);
          .put (sCacheKeyret);
        }
      }
      finally
      {
        .writeLock ().unlock ();
      }
    }
    return ret;
  }
  public static EChange removeFromCache (@Nonnull final EWebSiteResourceType eType,
                                         @Nonnull @Nonempty final String sPath)
  {
    ValueEnforcer.notNull (eType"Type");
    ValueEnforcer.notEmpty (sPath"Path");
    final String sCacheKey = eType.getID () + "-" + sPath;
    .writeLock ().lock ();
    try
    {
      return EChange.valueOf (.remove (sCacheKey) != null);
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }

  
Remove all items contained in the cache.

  public static EChange clearCache ()
  {
    .writeLock ().lock ();
    try
    {
      if (.isEmpty ())
        return .;
      .clear ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
    return .;
  }
New to GrepCode? Check out our FAQ X