protected void addEventToCache(Object event, String topic, Object eventObj) {
      //Taking the listener lock here, since a listener that is now subscribing will want
      //this event since they are not in this subscriber list.
      synchronized (listenerLock) {
         if (event != null) {
            int cacheSizeForEventClass = getCacheSizeForEventClass(event.getClass());
            List eventClassCache = (List) cacheByEvent.get(event.getClass());
            if (cacheSizeForEventClass <= 0) {
               if (eventClassCache != null) {
                  //the cache threshold was lowered to 0
                  cacheByEvent.remove(event.getClass());
               }
            } else {
               if (eventClassCache == null) {
                  eventClassCache = new LinkedList();
                  cacheByEvent.put(event.getClass(), eventClassCache);
               }
               eventClassCache.add(0, event);
               while (eventClassCache.size() > cacheSizeForEventClass) {
                  eventClassCache.remove(eventClassCache.size() - 1);
               }
            }
         } else {
            //topic
            int cacheSizeForTopic = getCacheSizeForTopic(topic);
            List topicCache = (List) cacheByTopic.get(topic);
            if (cacheSizeForTopic <= 0) {
               if (topicCache != null) {
                  //the cache threshold was lowered to 0
                  topicCache.remove(topic);
               }
            } else {
               if (topicCache == null) {
                  topicCache = new LinkedList();
                  cacheByTopic.put(topic, topicCache);
               }
               topicCache.add(0, eventObj);
               while (topicCache.size() > cacheSizeForTopic) {
                  topicCache.remove(topicCache.size() - 1);
               }
            }
         }
      }
   }