Mercurial Hosting > nabble
comparison src/nabble/view/lib/Cache.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 /* | |
2 | |
3 Copyright (C) 2001 Franklin Schmidt <frank@gustos.com> | |
4 | |
5 */ | |
6 | |
7 package nabble.view.lib; | |
8 | |
9 import fschmidt.db.Listener; | |
10 import fschmidt.util.servlet.HttpCache; | |
11 import fschmidt.util.servlet.JtpContext; | |
12 import nabble.model.FileUpload; | |
13 import nabble.model.Message; | |
14 import nabble.model.ModelHome; | |
15 import nabble.model.Executors; | |
16 import nabble.model.Node; | |
17 import nabble.model.Site; | |
18 import nabble.model.User; | |
19 import org.slf4j.Logger; | |
20 import org.slf4j.LoggerFactory; | |
21 | |
22 import java.util.HashSet; | |
23 import java.util.Set; | |
24 import java.util.concurrent.TimeUnit; | |
25 | |
26 | |
27 public final class Cache { | |
28 private static final Logger logger = LoggerFactory.getLogger(Cache.class); | |
29 | |
30 private static final int nodeEvent = 1; | |
31 private static final int descendantChangeEvent = 2; | |
32 // private static final int topicEvent = 3; | |
33 // private static final int postEvent = 4; | |
34 // private static final int skinEvent = 5; | |
35 // private static final int activityEvent = 6; | |
36 private static final int regularEvent = 7; | |
37 private static final int nodeFileEvent = 9; | |
38 // private static final int forumFileEvent = 10; | |
39 // private static final int topicRootEvent = 11; | |
40 private static final int userFileEvent = 12; | |
41 private static final int siteChangeEvent = 13; | |
42 private static final int newUserEvent = 14; | |
43 private static final int groupChangeEvent = 15; | |
44 | |
45 private static String event(int type,long id) { | |
46 return Integer.toString(type) + ':' + Long.toString(id); | |
47 } | |
48 | |
49 private static String event(int type,long id1,long id2) { | |
50 return Integer.toString(type) + ':' + Long.toString(id1) + ':' + Long.toString(id2); | |
51 } | |
52 | |
53 public static String descendantChangeEvent(Node node) { | |
54 return event(descendantChangeEvent,node.getSite().getId(),node.getId()); | |
55 } | |
56 | |
57 public static String nodeChangeEvent(Node node) { | |
58 return event(nodeEvent,node.getSite().getId(),node.getId()); | |
59 } | |
60 | |
61 public static String nodeFileChangeEvent(Node node) { | |
62 return event(nodeFileEvent,node.getSite().getId(),node.getId()); | |
63 } | |
64 | |
65 public static String userFileChangeEvent(User user) { | |
66 return event(userFileEvent,user.getSite().getId(), user.getId()); | |
67 } | |
68 | |
69 public static String siteChangeEvent(Site site) { | |
70 return event(siteChangeEvent, site.getId()); | |
71 } | |
72 | |
73 public static String newUserEvent(Site site) { | |
74 return event(newUserEvent, site.getId()); | |
75 } | |
76 | |
77 public static String groupChangeEvent(Site site) { | |
78 return event(groupChangeEvent, site.getId()); | |
79 } | |
80 | |
81 | |
82 static { | |
83 | |
84 ModelHome.addDescendantChangeListener(new Listener<Node>() { | |
85 public void event(Node node) { | |
86 uncache( Cache.descendantChangeEvent(node) ); | |
87 } | |
88 } ); | |
89 | |
90 ModelHome.addNodeChangeListener(new Listener<Node>() { | |
91 public void event(Node node) { | |
92 uncache( Cache.nodeChangeEvent(node) ); | |
93 } | |
94 } ); | |
95 | |
96 FileUpload.addFileUpdateListener(new Listener<Message.Source>() { | |
97 public void event(Message.Source src) { | |
98 char type = src.getMessageSourceType().getCode(); | |
99 switch(type) { | |
100 case 't': | |
101 return; | |
102 case 'a': // for now -fschmidt | |
103 uncache( Cache.userFileChangeEvent(((Message.AvatarSource)src).getUser()) ); | |
104 return; | |
105 case 'n': | |
106 uncache( Cache.nodeFileChangeEvent((Node)src) ); | |
107 return; | |
108 case 's': | |
109 uncache( Cache.siteChangeEvent((Site)src) ); | |
110 return; | |
111 default: | |
112 throw new RuntimeException("type = '"+type+"'"); | |
113 } | |
114 } | |
115 } ); | |
116 | |
117 ModelHome.addSiteChangeListener(new Listener<Site>() { | |
118 public void event(Site site) { | |
119 uncache( Cache.siteChangeEvent(site) ); | |
120 } | |
121 } ); | |
122 | |
123 ModelHome.addUserInsertListener(new Listener<User>() { | |
124 public void event(User user) { | |
125 uncache( Cache.newUserEvent(user.getSite()) ); | |
126 } | |
127 } ); | |
128 | |
129 Permissions.addGroupChangeListener(new Listener<Site>() { | |
130 public void event(Site site) { | |
131 uncache( Cache.newUserEvent(site) ); | |
132 } | |
133 } ); | |
134 | |
135 Permissions.addPermissionChangeListener(new Listener<Site>() { | |
136 public void event(Site site) { | |
137 uncache( Cache.siteChangeEvent(site) ); | |
138 } | |
139 } ); | |
140 | |
141 } | |
142 | |
143 public static void uncache(String event) { | |
144 HttpCache cache = MyJtpServlet.getJtpContextNotNull().getHttpCache(); | |
145 if( cache != null ) | |
146 cache.modified(event); | |
147 } | |
148 | |
149 | |
150 private static Set<Long> secondSet = new HashSet<Long>(); | |
151 | |
152 public static synchronized String regularEvent(long seconds) { | |
153 final String event = event(regularEvent,seconds); | |
154 if( secondSet.add(seconds) ) { | |
155 Executors.scheduleWithFixedDelay(new Runnable(){public void run(){ | |
156 JtpContext context = MyJtpServlet.getJtpContext(); | |
157 if( context != null ) { | |
158 HttpCache cache = context.getHttpCache(); | |
159 if( cache != null ) { | |
160 cache.modified(event); | |
161 } | |
162 } | |
163 }},seconds,seconds,TimeUnit.SECONDS); | |
164 } | |
165 return event; | |
166 } | |
167 | |
168 private static final long secondsPerDay = 60*60*24; | |
169 public static final String dailyEvent = regularEvent(secondsPerDay); | |
170 | |
171 static void init() {} | |
172 } |