|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.logging.cache.support; |
| 19 | + |
| 20 | +import org.minbox.framework.api.boot.common.exception.ApiBootException; |
| 21 | +import org.minbox.framework.api.boot.plugin.logging.ApiBootLog; |
| 22 | +import org.minbox.framework.api.boot.plugin.logging.cache.LoggingCache; |
| 23 | +import org.springframework.util.ObjectUtils; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.List; |
| 28 | +import java.util.UUID; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.concurrent.ConcurrentMap; |
| 31 | + |
| 32 | +/** |
| 33 | + * ApiBoot Logging Memory Away Cache |
| 34 | + * |
| 35 | + * @author:恒宇少年 - 于起宇 |
| 36 | + * <p> |
| 37 | + * DateTime:2019-07-21 13:50 |
| 38 | + * Blog:http://blog.yuqiyu.com |
| 39 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 40 | + * Gitee:https://gitee.com/hengboy |
| 41 | + * GitHub:https://github.com/hengboy |
| 42 | + */ |
| 43 | +public class LoggingMemoryCache implements LoggingCache { |
| 44 | + /** |
| 45 | + * Cache ApiBootLog Map |
| 46 | + * For Batch Report |
| 47 | + */ |
| 48 | + private static final ConcurrentMap<String, ApiBootLog> CACHE_LOGS = new ConcurrentHashMap(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Cache Single ApiBootLog |
| 52 | + * |
| 53 | + * @param log ApiBootLog |
| 54 | + * @throws ApiBootException ApiBoot Exception |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public void cache(ApiBootLog log) throws ApiBootException { |
| 58 | + if (!ObjectUtils.isEmpty(log)) { |
| 59 | + CACHE_LOGS.put(UUID.randomUUID().toString(), log); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get Any One ApiBootLog |
| 65 | + * |
| 66 | + * @return ApiBootLog |
| 67 | + * @throws ApiBootException ApiBoot Exception |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public ApiBootLog getAnyOne() throws ApiBootException { |
| 71 | + List<ApiBootLog> logs = get(0); |
| 72 | + return logs.size() > 0 ? logs.get(0) : null; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets the specified number of ApiBootLog |
| 77 | + * |
| 78 | + * @param count get count number |
| 79 | + * @return ApiBootLog Collection |
| 80 | + * @throws ApiBootException ApiBoot Exception |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public List<ApiBootLog> getLogs(int count) throws ApiBootException { |
| 84 | + if (CACHE_LOGS.size() >= count) { |
| 85 | + return get(count); |
| 86 | + } |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets All Of ApiBootLog |
| 92 | + * |
| 93 | + * @return ApiBootLog Collection |
| 94 | + * @throws ApiBootException ApiBoot Exception |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public List<ApiBootLog> getAll() throws ApiBootException { |
| 98 | + return get(null); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get ApiBootLogs |
| 103 | + * |
| 104 | + * @param count get count number |
| 105 | + * @return ApiBootLog Collection |
| 106 | + * @throws ApiBootException ApiBoot Exception |
| 107 | + */ |
| 108 | + private List<ApiBootLog> get(Integer count) throws ApiBootException { |
| 109 | + List<ApiBootLog> logs = new ArrayList(); |
| 110 | + Iterator<String> iterator = CACHE_LOGS.keySet().iterator(); |
| 111 | + int index = 0; |
| 112 | + while (iterator.hasNext()) { |
| 113 | + String key = iterator.next(); |
| 114 | + logs.add(CACHE_LOGS.get(key)); |
| 115 | + CACHE_LOGS.remove(key); |
| 116 | + if (count != null && index >= count - 1) { |
| 117 | + break; |
| 118 | + } |
| 119 | + index++; |
| 120 | + } |
| 121 | + return logs; |
| 122 | + } |
| 123 | +} |
0 commit comments