| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | package datasources | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2019-06-13 16:55:38 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/localcache" | 
					
						
							| 
									
										
										
										
											2020-02-29 20:35:15 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/registry" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CacheService interface { | 
					
						
							| 
									
										
										
										
											2020-02-29 20:35:15 +08:00
										 |  |  | 	GetDatasource(datasourceID int64, user *models.SignedInUser, skipCache bool) (*models.DataSource, error) | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type CacheServiceImpl struct { | 
					
						
							| 
									
										
										
										
											2019-06-13 16:55:38 +08:00
										 |  |  | 	Bus          bus.Bus                  `inject:""` | 
					
						
							|  |  |  | 	CacheService *localcache.CacheService `inject:""` | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2018-10-30 19:32:14 +08:00
										 |  |  | 	registry.Register(®istry.Descriptor{ | 
					
						
							|  |  |  | 		Name:         "DatasourceCacheService", | 
					
						
							|  |  |  | 		Instance:     &CacheServiceImpl{}, | 
					
						
							|  |  |  | 		InitPriority: registry.Low, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dc *CacheServiceImpl) Init() error { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 20:35:15 +08:00
										 |  |  | func (dc *CacheServiceImpl) GetDatasource(datasourceID int64, user *models.SignedInUser, skipCache bool) (*models.DataSource, error) { | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	cacheKey := fmt.Sprintf("ds-%d", datasourceID) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !skipCache { | 
					
						
							|  |  |  | 		if cached, found := dc.CacheService.Get(cacheKey); found { | 
					
						
							| 
									
										
										
										
											2020-02-29 20:35:15 +08:00
										 |  |  | 			ds := cached.(*models.DataSource) | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 			if ds.OrgId == user.OrgId { | 
					
						
							|  |  |  | 				return ds, nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 20:35:15 +08:00
										 |  |  | 	query := models.GetDataSourceByIdQuery{Id: datasourceID, OrgId: user.OrgId} | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	if err := dc.Bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dc.CacheService.Set(cacheKey, query.Result, time.Second*5) | 
					
						
							|  |  |  | 	return query.Result, nil | 
					
						
							|  |  |  | } |