`RedisOperationsSessionRepository` is a `SessionRepository` that is implemented using Spring Data's `RedisOperations`. In a web environment, this is typically used in combination with `SessionRepositoryFilter`. The implementation supports `SessionDeletedEvent` and `SessionExpiredEvent` through `SessionMessageListener`. Categories help learners locate content, by applying category filters. In the COMPLETION section, you can configure the completion behaviour of the item. The following options are available: Automatic and manual. Enrolled learners are automatically marked as having attended the event, after the end of.
AppengineConfig.java
importcom.google.appengine.api.memcache.MemcacheService; |
importcom.google.appengine.api.memcache.MemcacheServiceFactory; |
importcom.google.appengine.api.utils.SystemProperty; |
importcom.google.auth.oauth2.GoogleCredentials; |
importcom.google.cloud.datastore.Datastore; |
importcom.google.cloud.datastore.DatastoreOptions; |
importcom.google.common.base.Strings; |
importlombok.extern.slf4j.Slf4j; |
importorg.springframework.beans.factory.annotation.Value; |
importorg.springframework.context.annotation.Bean; |
importorg.springframework.context.annotation.Configuration; |
importorg.springframework.core.io.ClassPathResource; |
importorg.springframework.session.web.http.SessionRepositoryFilter; |
importjava.io.IOException; |
@Configuration |
publicclassAppengineConfig { |
@Bean |
publicMemcacheServicememcacheService() { |
returnMemcacheServiceFactory.getMemcacheService('_mcsession'); |
} |
@Bean |
publicSessionRepositoryFilter<MemcacheSession>sessionRepositoryFilter(MemcacheSessionRepositorymemcacheSessionRepository) { |
returnnewSessionRepositoryFilter<>(memcacheSessionRepository); |
} |
@Bean |
publicMemcacheSessionRepositorymemcacheSessionRepository(MemcacheServicememcacheService) { |
returnnewMemcacheSessionRepository(memcacheService); |
} |
} |
MemcacheSession.java
importlombok.Data; |
importorg.springframework.session.ExpiringSession; |
importjava.io.Serializable; |
importjava.util.HashMap; |
importjava.util.Map; |
importjava.util.Set; |
importjava.util.UUID; |
import staticjava.util.concurrent.TimeUnit.MILLISECONDS; |
import staticjava.util.concurrent.TimeUnit.SECONDS; |
@Data |
publicclassMemcacheSessionimplementsSerializable, ExpiringSession { |
privateString id =UUID.randomUUID().toString(); |
privatelong creationTime =System.currentTimeMillis(); |
privatelong lastAccessedTime = creationTime; |
privateint maxInactiveIntervalInSeconds =3600*24; |
privateMap<String, Object> attributes =newHashMap<>(); |
@Override |
publicbooleanisExpired() { |
long lastAccessSeconds =MILLISECONDS.convert(lastAccessedTime, SECONDS); |
long expiryTime = lastAccessSeconds + maxInactiveIntervalInSeconds; |
long currentTime =MILLISECONDS.convert(System.currentTimeMillis(), SECONDS); |
return currentTime > expiryTime; |
} |
@Override |
public<T>TgetAttribute(StringattributeName) { |
return (T) attributes.get(attributeName); |
} |
@Override |
publicSet<String>getAttributeNames() { |
return attributes.keySet(); |
} |
@Override |
publicvoidsetAttribute(StringattributeName, ObjectattributeValue) { |
attributes.put(attributeName, attributeValue); |
} |
@Override |
publicvoidremoveAttribute(StringattributeName) { |
attributes.remove(attributeName); |
} |
publicvoidsetLastAccessedTimeToNow() { |
this.lastAccessedTime =System.currentTimeMillis(); |
} |
} |
MemcacheSessionRepository.java
importcom.google.appengine.api.memcache.Expiration; |
importcom.google.appengine.api.memcache.MemcacheService; |
importlombok.extern.slf4j.Slf4j; |
importorg.springframework.session.SessionRepository; |
@Slf4j |
publicclassMemcacheSessionRepositoryimplementsSessionRepository<MemcacheSession> { |
privatefinalMemcacheService memcacheService; |
privateint maxInactiveIntervalInSeconds =3600*24; |
publicMemcacheSessionRepository(MemcacheServicememcacheService) { |
this.memcacheService = memcacheService; |
} |
@Override |
publicMemcacheSessioncreateSession() { |
MemcacheSession memcacheSession =newMemcacheSession(); |
memcacheSession.setMaxInactiveIntervalInSeconds(maxInactiveIntervalInSeconds); |
log.debug('Created new memcache session: {}', memcacheSession); |
return memcacheSession; |
} |
@Override |
publicvoidsave(MemcacheSessionmemcacheSession) { |
memcacheService.put(memcacheSession.getId(), memcacheSession, Expiration.byDeltaSeconds(memcacheSession.getMaxInactiveIntervalInSeconds())); |
log.debug('Saved session in memcache: {}', memcacheSession); |
} |
@Override |
publicMemcacheSessiongetSession(StringsessionId) { |
MemcacheSession session = (MemcacheSession) memcacheService.get(sessionId); |
if (session null) { |
log.warn('Requested session does not exist in memcache: {}', sessionId); |
returnnull; |
} |
session.setLastAccessedTimeToNow(); |
return session; |
} |
@Override |
publicvoiddelete(StringsessionId) { |
memcacheService.delete(sessionId); |
} |
publicvoidsetMaxInactiveIntervalInSeconds(intmaxInactiveIntervalInSeconds) { |
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds; |
} |
} |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment