001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Gagnavarslan ehf 018 */ 019package org.nuxeo.ecm.webdav.backend; 020 021import java.time.Duration; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.concurrent.ConcurrentHashMap; 025 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentNotFoundException; 029import org.nuxeo.ecm.core.api.IdRef; 030 031/** 032 * @deprecated since 10.2, unused 033 */ 034@Deprecated 035public class PathCache { 036 037 private static final long FOLDER_LIFE_TIME = Duration.ofMinutes(30).toMillis(); 038 039 private static final long FILE_LIFE_TIME = Duration.ofMinutes(1).toMillis(); 040 041 private int maxSize; 042 043 private Map<String, Value> pathToUuidCache = new ConcurrentHashMap<>(); 044 045 private CoreSession session; 046 047 public PathCache(CoreSession session, int maxSize) { 048 this.session = session; 049 this.maxSize = maxSize; 050 } 051 052 public void put(String path, DocumentModel model) { 053 if (model == null) { 054 return; 055 } 056 if (pathToUuidCache.size() >= maxSize) { 057 clean(); 058 } 059 pathToUuidCache.put(path, new Value(System.currentTimeMillis() 060 + (model.isFolder() ? FOLDER_LIFE_TIME : FILE_LIFE_TIME), model.getId())); 061 } 062 063 public DocumentModel get(String path) { 064 Value value = pathToUuidCache.get(path); 065 if (value == null) { 066 return null; 067 } 068 069 if (value.getExpiredTime() < System.currentTimeMillis()) { 070 pathToUuidCache.remove(path); 071 return null; 072 } 073 String uuid = value.getValue(); 074 DocumentModel model = null; 075 try { 076 model = session.getDocument(new IdRef(uuid)); 077 } catch (DocumentNotFoundException e) { 078 // do nothing 079 } 080 if (model == null) { 081 pathToUuidCache.remove(path); 082 } 083 084 return model; 085 } 086 087 public void remove(String path) { 088 Map<String, Value> cacheCopy = new HashMap<>(pathToUuidCache); 089 for (String key : cacheCopy.keySet()) { 090 if (key.startsWith(path)) { 091 pathToUuidCache.remove(key); 092 } 093 } 094 } 095 096 private int clean() { 097 Map<String, Value> tmpMap = new HashMap<>(pathToUuidCache); 098 for (String key : tmpMap.keySet()) { 099 if ((pathToUuidCache.get(key).getExpiredTime()) < System.currentTimeMillis()) { 100 remove(key); 101 } 102 } 103 return pathToUuidCache.size(); 104 } 105 106 private class Value { 107 private long expiredTime; 108 109 private String value; 110 111 private Value(long expiredTime, String value) { 112 this.expiredTime = expiredTime; 113 this.value = value; 114 } 115 116 public long getExpiredTime() { 117 return expiredTime; 118 } 119 120 public String getValue() { 121 return value; 122 } 123 } 124 125}