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: 30 inserted, 13 deleted

@@ -4,20 +4,27
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)
14 - if @@configurations == nil
17 + if @@cache
15 - self.read_config
18 + if @@configurations == nil
19 + self.read_config
20 + end
21 + return @@configurations[key]
22 + else
23 + return Configuration.read_one_key(key)
16 end
24 end
17 - return @@configurations[key]
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
@@ -68,28 +75,38
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'
101 + end
81 - @@configurations[key] = val
102 + end
82
103
83 - when 'integer'
104 + def self.read_one_key(key)
84 - @@configurations[key] = val.to_i
105 + conf = Configuration.find_by_key(key)
85 -
106 + return Configuration.convert_type(conf.value,conf.value_type)
86 - when 'boolean'
87 - @@configurations[key] = (val=='true')
88 - end
89 - end
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
You need to be logged in to leave comments. Login now