Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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 org.apache.marmotta.ldcache.services;
 
 
 import java.util.Date;
 import java.util.Set;
Next generation LDCache API. Will eventually replace the old LDCache API.

Author(s):
Sebastian Schaffert (sschaffert@apache.org)
 
 public class LDCache implements LDCachingService {
 
     private static Logger log = LoggerFactory.getLogger(LDCache.class);
 
     // lock a resource while refreshing it so that not several threads trigger a refresh at the same time
     private ObjectLocks resourceLocks;
 
     private CacheConfiguration config;
 
     private LDClientService ldclient;
 
     private LDCachingBackend backend;
 
     private ReentrantReadWriteLock lock;

    
Create a new instance of LDCache using the provided LDCache configuration and backend. The backend needs to be initialized already. The cache configuration will be used to create an instance of LDClient.

Parameters:
config
backend
 
     public LDCache(CacheConfiguration configLDCachingBackend backend) {
         this. = new ObjectLocks();
         this.  = backend;
         this. = new LDClient(config.getClientConfiguration());
         this.   = config;
         this. = new ReentrantReadWriteLock();
     }


    
Reload configuration and initialise LDClient.
 
     public void reload() {
         .writeLock().lock();
         try {
             if(this. != null) {
                 .info("Reloading LDClient configuration ...");
                 this..shutdown();
                 this. = new LDClient(.getClientConfiguration());
             }
         } finally {
             .writeLock().unlock();
         }
     }

    
Refresh the resource passed as argument. If the resource is not yet cached or the cache entry is expired or refreshing is forced, the remote resource is retrieved using LDClient and the result stored in the cache. Otherwise the method does nothing.

Parameters:
resource the resource to refresh
options options for refreshing
    @Override
    public void refresh(URI resourceRefreshOpts... options) {
        Set<RefreshOptsoptionSet = new HashSet<>(Arrays.asList(options));
        .lock(resource.stringValue());
        try {
            // check if the resource is already cached; if yes, and refresh is not forced, return immediately
            CacheEntry entry = .getEntry(resource);
            if(!optionSet.contains(.) && entry != null && entry.getExpiryDate().after(new Date())) {
                .debug("not refreshing resource {}, as the cached entry is not yet expired",resource);
                return;
            }
            // refresh the resource by calling LDClient
            .debug("refreshing resource {}",resource);
            this..readLock().lock();
            try {
                ClientResponse response = .retrieveResource(resource.stringValue());
                if(response != null) {
                    .info("refreshed resource {}",resource);
                    CacheEntry newEntry = new CacheEntry();
                    newEntry.setResource(resource);
                    newEntry.setExpiryDate(response.getExpires());
                    newEntry.setLastRetrieved(new Date());
                    if(entry != null) {
                        newEntry.setUpdateCount(entry.getUpdateCount()+1);
                    } else {
                        newEntry.setUpdateCount(1);
                    }
                    newEntry.setTripleCount(response.getData().size());
                    newEntry.setTriples(response.getData());
                    .putEntry(resourcenewEntry);
                }
            } catch (DataRetrievalException e) {
                // on exception, save an expiry information and retry in one day
                CacheEntry newEntry = new CacheEntry();
                newEntry.setResource(resource);
                newEntry.setExpiryDate(new Date(System.currentTimeMillis() + .getDefaultExpiry()*1000));
                newEntry.setLastRetrieved(new Date());
                if(entry != null) {
                    newEntry.setUpdateCount(entry.getUpdateCount()+1);
                } else {
                    newEntry.setUpdateCount(1);
                }
                newEntry.setTripleCount(0);
                newEntry.setTriples(new TreeModel());
                .putEntry(resourcenewEntry);
            } finally {
                this..readLock().unlock();
            }
        } finally {
            .unlock(resource.stringValue());
        }
    }

    
Refresh and return the resource passed as argument. If the resource is not yet cached or the cache entry is expired or refreshing is forced, the remote resource is retrieved using LDClient and the result stored in the cache. Otherwise the method returns the cached entry. In case a cached entry does not exist, the method returns an empty Model.

Parameters:
resource the resource to retrieve
options options for refreshing
Returns:
a Sesame Model holding the triples representing the resource
    @Override
    public Model get(URI resourceRefreshOpts... options) {
        refresh(resourceoptions);
        CacheEntry entry =  .getEntry(resource);
        if(entry != null) {
            return entry.getTriples();
        } else {
            return new TreeModel();
        }
    }

    
Manually expire the caching information for the given resource. The resource will be re-retrieved upon the next access.

Parameters:
resource the resource to expire.
    @Override
    public void expire(URI resource) {
        .removeEntry(resource);
    }

    
Return true in case the cache contains an entry for the resource given as argument.

Parameters:
resource the resource to check
Returns:
true in case the resource is contained in the cache
    @Override
    public boolean contains(URI resource) {
        return .getEntry(resource) != null;
    }

    
Manually expire all cached resources.
    @Override
    public void clear() {
        .clear();
    }

    
Shutdown the caching service and free all occupied runtime resources.
    @Override
    public void shutdown() {
        .shutdown();
    }
    public LDClientService getClient() {
        return ;
    }
New to GrepCode? Check out our FAQ X