Description:
removed caching on Configuration git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@285 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r146:1d03356b1f43 - - 1 file changed: 26 inserted, 9 deleted

@@ -1,41 +1,48
1 require 'yaml'
1 require 'yaml'
2
2
3 #
3 #
4 # This class also contains various login of the system.
4 # This class also contains various login of the system.
5 #
5 #
6 class Configuration < ActiveRecord::Base
6 class Configuration < ActiveRecord::Base
7
7
8 SYSTEM_MODE_CONF_KEY = 'system.mode'
8 SYSTEM_MODE_CONF_KEY = 'system.mode'
9
9
10 + # set @@cache = true to only reload once.
11 + @@cache = false
12 +
10 @@configurations = nil
13 @@configurations = nil
11 @@task_grading_info = nil
14 @@task_grading_info = nil
12
15
13 def self.get(key)
16 def self.get(key)
17 + if @@cache
14 if @@configurations == nil
18 if @@configurations == nil
15 self.read_config
19 self.read_config
16 end
20 end
17 return @@configurations[key]
21 return @@configurations[key]
22 + else
23 + return Configuration.read_one_key(key)
24 + end
18 end
25 end
19
26
20 def self.[](key)
27 def self.[](key)
21 self.get(key)
28 self.get(key)
22 end
29 end
23
30
24 def self.reload
31 def self.reload
25 self.read_config
32 self.read_config
26 end
33 end
27
34
28 def self.clear
35 def self.clear
29 @@configurations = nil
36 @@configurations = nil
30 end
37 end
31
38
32 #
39 #
33 # View decision
40 # View decision
34 #
41 #
35 def self.show_submitbox_to?(user)
42 def self.show_submitbox_to?(user)
36 mode = get(SYSTEM_MODE_CONF_KEY)
43 mode = get(SYSTEM_MODE_CONF_KEY)
37 return false if mode=='analysis'
44 return false if mode=='analysis'
38 if (mode=='contest')
45 if (mode=='contest')
39 return false if (user.site!=nil) and
46 return false if (user.site!=nil) and
40 ((user.site.started!=true) or (user.site.finished?))
47 ((user.site.started!=true) or (user.site.finished?))
41 end
48 end
@@ -50,49 +57,59
50 return true
57 return true
51 end
58 end
52
59
53 def self.show_grading_result
60 def self.show_grading_result
54 return (get(SYSTEM_MODE_CONF_KEY)=='analysis')
61 return (get(SYSTEM_MODE_CONF_KEY)=='analysis')
55 end
62 end
56
63
57 def self.allow_test_request(user)
64 def self.allow_test_request(user)
58 mode = get(SYSTEM_MODE_CONF_KEY)
65 mode = get(SYSTEM_MODE_CONF_KEY)
59 if (mode=='contest')
66 if (mode=='contest')
60 return false if (user.site!=nil) and ((user.site.started!=true) or (user.site.time_left < 30.minutes))
67 return false if (user.site!=nil) and ((user.site.started!=true) or (user.site.time_left < 30.minutes))
61 end
68 end
62 return false if mode=='analysis'
69 return false if mode=='analysis'
63 return true
70 return true
64 end
71 end
65
72
66 def self.task_grading_info
73 def self.task_grading_info
67 if @@task_grading_info==nil
74 if @@task_grading_info==nil
68 read_grading_info
75 read_grading_info
69 end
76 end
70 return @@task_grading_info
77 return @@task_grading_info
71 end
78 end
72
79
73 protected
80 protected
81 +
82 + def self.convert_type(val,type)
83 + case type
84 + when 'string'
85 + return val
86 +
87 + when 'integer'
88 + return val.to_i
89 +
90 + when 'boolean'
91 + return (val=='true')
92 + end
93 + end
94 +
74 def self.read_config
95 def self.read_config
75 @@configurations = {}
96 @@configurations = {}
76 Configuration.find(:all).each do |conf|
97 Configuration.find(:all).each do |conf|
77 key = conf.key
98 key = conf.key
78 val = conf.value
99 val = conf.value
79 - case conf.value_type
100 + @@configurations[key] = Configuration.convert_type(val,conf.value_type)
80 - when 'string'
81 - @@configurations[key] = val
82 -
83 - when 'integer'
84 - @@configurations[key] = val.to_i
85 -
86 - when 'boolean'
87 - @@configurations[key] = (val=='true')
88 end
101 end
89 end
102 end
103 +
104 + def self.read_one_key(key)
105 + conf = Configuration.find_by_key(key)
106 + return Configuration.convert_type(conf.value,conf.value_type)
90 end
107 end
91
108
92 def self.read_grading_info
109 def self.read_grading_info
93 f = File.open(TASK_GRADING_INFO_FILENAME)
110 f = File.open(TASK_GRADING_INFO_FILENAME)
94 @@task_grading_info = YAML.load(f)
111 @@task_grading_info = YAML.load(f)
95 f.close
112 f.close
96 end
113 end
97
114
98 end
115 end
You need to be logged in to leave comments. Login now