1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 module hunt.shiro.session.mgt.eis.MemorySessionDAO; 20 21 import hunt.shiro.session.mgt.eis.AbstractSessionDAO; 22 23 import hunt.shiro.session.Session; 24 import hunt.shiro.Exceptions; 25 import hunt.shiro.util.CollectionUtils; 26 27 import hunt.collection; 28 import hunt.Exceptions; 29 import hunt.logging.Logger; 30 import hunt.util.Common; 31 32 /** 33 * Simple memory-based implementation of the SessionDAO that stores all of its sessions in an in-memory 34 * {@link ConcurrentMap}. <b>This implementation does not page to disk and is therefore unsuitable for applications 35 * that could experience a large amount of sessions</b> and would therefore cause {@code OutOfMemoryException}s. It is 36 * <em>not</em> recommended for production use in most environments. 37 * <h2>Memory Restrictions</h2> 38 * If your application is expected to host many sessions beyond what can be stored in the 39 * memory available to the JVM, it is highly recommended to use a different {@code SessionDAO} implementation which 40 * uses a more expansive or permanent backing data store. 41 * <p/> 42 * In this case, it is recommended to instead use a custom 43 * {@link CachingSessionDAO} implementation that communicates with a higher-capacity data store of your choice 44 * (file system, database, etc). 45 * <h2>Changes in 1.0</h2> 46 * This implementation prior to 1.0 used to subclass the {@link CachingSessionDAO}, but this caused problems with many 47 * cache implementations that would expunge entries due to TTL settings, resulting in Sessions that would be randomly 48 * (and permanently) lost. The Shiro 1.0 release refactored this implementation to be 100% memory-based (without 49 * {@code Cache} usage to avoid this problem. 50 * 51 * @see CachingSessionDAO 52 */ 53 class MemorySessionDAO : AbstractSessionDAO { 54 55 private Map!(string, Session) sessions; 56 57 this() { 58 this.sessions = new HashMap!(string, Session)(); 59 } 60 61 override protected string doCreate(Session session) { 62 string sessionId = generateSessionId(session); 63 assignSessionId(session, sessionId); 64 storeSession(sessionId, session); 65 return sessionId; 66 } 67 68 protected Session storeSession(string id, Session session) { 69 if (id is null) { 70 throw new NullPointerException("id argument cannot be null."); 71 } 72 return sessions.putIfAbsent(id, session); 73 } 74 75 override protected Session doReadSession(string sessionId) { 76 return sessions.get(sessionId); 77 } 78 79 void update(Session session){ 80 storeSession(session.getId(), session); 81 } 82 83 void remove(Session session) { 84 if (session is null) { 85 throw new NullPointerException("session argument cannot be null."); 86 } 87 string id = session.getId(); 88 if (id !is null) { 89 sessions.remove(id); 90 } 91 } 92 93 Session[] getActiveSessions() { 94 Session[] values = sessions.values(); 95 return values; 96 } 97 98 }